简体   繁体   中英

How to insert dan update value from HTML <select> multiple Attributes in ASP.NET MVC to database?

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; }
}

您可以将它们发送到后端逗号分隔,如 page1,page2,page3 或者更简单的更好的方法创建包含所有带有 ID 的页面的表查找并将 ID 存储在数据库中

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM