0

How do I input multiple values to the database from HTML multiple Attributes? In this problem I want to input multiple values in MultiSelectList in one column in the database table, namely the Pages column. And then about the form update to display the options that have been previously selected? I am using ASP.NET MVC with a MySQL database. Thank's

Create.cshtml

<div class="box-body">
    <form class="form-horizontal" method="post" action="CreatePage">
        <table class="table table-striped">
            <tr>
               <td>Page</td>
               <td><select class="form-control select2" multiple="multiple" style="width: 100%;" name="Page">
                   <option value="Home">Home</option>
                   <option value="About">About</option>
                   <option value="Profile">Profile</option>
                  </select>
               </td>
            </tr>
        </table>                
      <div class="box-footer">
      <button type="submit" class="btn btn-primary">Submit</button>
     </div>
   </form>
 </div> 

CreateController.cs

[HttpPost]
public ActionResult CreatePage(MasterPage Page)
{
    db.MasterPage.Add(Page);
    db.SaveChanges();
    return RedirectToAction("Index","Home");
}

public ActionResult Create()
{
    return View();
}

MasterPage.cs

public partial class MasterPage
{
   public string Page { get; set; }
}

2 Answers 2

0

You can use string[] array or List<string>, for sending multiple attributes values.

For more details check this Link

View Code

<div class="box-body">
    <form class="form-horizontal" method="post" action="CreatePage">
        <table class="table table-striped">
            <tr>
               <td>Page</td>
               <td><select class="form-control select2" multiple="multiple" style="width: 100%;" name="Page">
                   <option value="Home">Home</option>
                   <option value="About">About</option>
                   <option value="Profile">Profile</option>
                  </select>
               </td>
            </tr>
        </table>                
      <div class="box-footer">
      <button type="submit" class="btn btn-primary">Submit</button>
     </div>
   </form>
 </div> 

Controller Code

[HttpPost]
public ActionResult CreatePage(List<string> Page)
{           
    if (Page != null)
    {
        foreach(var item in Page)
        {
            db.MasterPage.Add(new MasterPage(){ Page = item});
            db.SaveChanges();
        }
    }
    return RedirectToAction("Index", "Home");
}
public ActionResult Create()
{
    return View();
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can send them to back-end comma separated like page1,page2,page3 or the easy better way create table lookup that contain all the pages with IDs and store just the IDs in database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.