Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从HTML插入dan更新值<;选择>;ASP.NET MVC中的多个属性到数据库?_C#_Html_Mysql_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 如何从HTML插入dan更新值<;选择>;ASP.NET MVC中的多个属性到数据库?

C# 如何从HTML插入dan更新值<;选择>;ASP.NET MVC中的多个属性到数据库?,c#,html,mysql,asp.net,asp.net-mvc,C#,Html,Mysql,Asp.net,Asp.net Mvc,如何从HTML多属性向数据库输入多个值?在这个问题中,我想在数据库表的一列(即Pages列)的MultiSelectList中输入多个值。然后关于表单更新以显示以前选择的选项?我正在使用ASP.NETMVC和MySQL数据库。谢谢 Create.cshtml <div class="box-body"> <form class="form-horizontal" method="post" action="CreatePage"> <tabl

如何从HTML多属性向数据库输入多个值?在这个问题中,我想在数据库表的一列(即Pages列)的MultiSelectList中输入多个值。然后关于表单更新以显示以前选择的选项?我正在使用ASP.NETMVC和MySQL数据库。谢谢

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> 
MasterPage.cs

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

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

您可以使用
字符串[]
数组或
列表
,来发送多个属性值

有关更多详细信息,请查看此

视图代码


页
家
关于
轮廓
提交
控制器代码

[HttpPost]
公共操作结果创建页面(列表页面)
{           
如果(第页!=null)
{
foreach(第页中的变量项)
{
添加(新母版页(){Page=item});
db.SaveChanges();
}
}
返回重定向到操作(“索引”、“主页”);
}
公共操作结果创建()
{
返回视图();
}
public partial class MasterPage
{
   public string Page { get; set; }
}
<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> 
[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();
}