Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Asp.net mvc 2 具有动态复选框数的ASP.NET MVC模型_Asp.net Mvc 2_C# 4.0_Viewmodel - Fatal编程技术网

Asp.net mvc 2 具有动态复选框数的ASP.NET MVC模型

Asp.net mvc 2 具有动态复选框数的ASP.NET MVC模型,asp.net-mvc-2,c#-4.0,viewmodel,Asp.net Mvc 2,C# 4.0,Viewmodel,我有一个一对多关系的数据库,我在ASP.NET MVC中很难建模。为了简单起见,假设表A代表办公楼,表B代表员工,表C创建员工和建筑之间的一对多关系,以指示哪些员工可以访问特定的建筑 Employee EmployeeId - int FirstName - string LastName - string Office OfficeId - int Location - string EmployeeOffice EmployeeId - int OfficeId - int [ ] -

我有一个一对多关系的数据库,我在ASP.NET MVC中很难建模。为了简单起见,假设表A代表办公楼,表B代表员工,表C创建员工和建筑之间的一对多关系,以指示哪些员工可以访问特定的建筑

Employee EmployeeId - int FirstName - string LastName - string Office OfficeId - int Location - string EmployeeOffice EmployeeId - int OfficeId - int [ ] - Office 1 [ ] - Office 2 [ ] - Office 3 [ ] - Office 4 我的观点如下

<div>
    <%: Html.LabelFor(model => model.FirstName) %>
</div>
<div>
    <%: Html.TextBoxFor(model => model.FirstName) %>
    <%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>

<div>
    <%: Html.LabelFor(model => model.LastName) %>
</div>
<div>
    <%: Html.TextBoxFor(model => model.LastName) %>
    <%: Html.ValidationMessageFor(model => model.LastName) %>
</div>

<!-- This is really ugly, so I welcome any suggested updates -->
<% foreach (KeyValuePair<int, string> office in Model.Offices) { %>
    <label for="Office<%: office.Key %>"><%: office.Value %></label>
    <input id="Office<%: office.Key %>" type="checkbox" value="<%: office.Key %>" />
<% } %>
添加FormCollection对象的原因是,我希望除了在视图模型上找到的表单值(例如,复选框)之外,还可以获得表单值。然而,不幸的是,集合只包含在我的视图模型中找到的属性的信息


在ASP.NET MVC 2.0中,用这种设计处理表单的正确方法是什么?

您需要在复选框输入元素中添加“name”属性,否则它们将不会显示在表单集合中。

我正式需要休息一下!我真不敢相信我错过了。非常感谢你!
<div>
    <%: Html.LabelFor(model => model.FirstName) %>
</div>
<div>
    <%: Html.TextBoxFor(model => model.FirstName) %>
    <%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>

<div>
    <%: Html.LabelFor(model => model.LastName) %>
</div>
<div>
    <%: Html.TextBoxFor(model => model.LastName) %>
    <%: Html.ValidationMessageFor(model => model.LastName) %>
</div>

<!-- This is really ugly, so I welcome any suggested updates -->
<% foreach (KeyValuePair<int, string> office in Model.Offices) { %>
    <label for="Office<%: office.Key %>"><%: office.Value %></label>
    <input id="Office<%: office.Key %>" type="checkbox" value="<%: office.Key %>" />
<% } %>
public ActionResult Create(CreateUserViewModel user, FormCollection collection)