问题描述
我有以下课程:
public class ControllerSecurityModel
{
public string ControlleName { get; set; }
public string DisplayName { get; set; }
public List<ActionSecurityModel> actions { get; set; }
}
public class ActionSecurityModel
{
public string ActionName { get; set; }
public string DisplayName { get; set; }
public bool IsChecked { get; set; }
}
和一个模型:
public class PageRoleModel
{
public List<ControllerSecurityModel> AllPages { get; set; }
public List<ControllerSecurityModel> SelectedPage { get; set; }
}
我要为每个 ActionSecurityModel都有一个复选框,在下面的视图中编写:
I want to have checkbox for each "ActionSecurityModel" , I write below view :
<% using (Html.BeginForm())
{%>
<% foreach (var cont in Model.AllPages)
{%>
<fieldset>
<legend>
<%= cont.DisplayName %></legend>
<% foreach (var act in cont.actions)
{%>
<%: Html.CheckBoxFor(x => act.IsChecked) %>
<%: Html.Label(act.DisplayName) %>
<% } %>
</fieldset>
<% } %>
<input type="submit" value="save"/>
<% } %>
这是我的控制器操作:
public ActionResult SetRole()
{
PageRoleModel model = new PageRoleModel();
return View(model);
}
[HttpPost]
public ActionResult SetRole(PageRoleModel model)
{
return View(model);
}
但是当我提交表格时该模型为空吗?
如何提交复选框并保存呢?
but when I submit form the model is null?How can I submit checkboxes and save them?
推荐答案
就像这样:
<% using (Html.BeginForm()) { %>
<% for (var i = 0; i < Model.AllPages.Count; i++) { %>
<fieldset>
<legend>
<%= Model.AllPages[i].DisplayName %>
</legend>
<% for (var j = 0; j < Model.AllPages[i].actions.Count; j++ ) { %>
<%= Html.CheckBoxFor(x => x.AllPages[i].actions[j].IsChecked) %>
<%= Html.Label(Model.AllPages[i].actions[j].DisplayName) %>
}
</fieldset>
<% } %>
<input type="submit" value="save"/>
<% } %>
要了解为什么我的解决方案有效而您的解决方案无效,请阅读预期的。然后,通过浏览生成的HTML源代码查看表单输入字段的生成名称-您将很快看到复选框 name
属性的根本区别。
To understand why my solution works and your not, please read about the expected wire format that the default model binder uses for collections. Then look at the generated names of the form input fields by browsing the generated HTML source code - you will quickly see a fundamental difference in the name
attribute of the checkbox.
也不要期望将整个模型绑定到POST操作中。您的表单中只有一个输入字段-一个复选框。因此,这是唯一要发送到服务器并绑定到模型的值。如果还需要其他值,则可以将它们包括为隐藏字段:
Also don't expect to get your entire model bound in the POST action. You only have a single input field inside your form - a checkbox. So that's the only value that's gonna be sent to the server and bound to the model. If you needed the other values as well you could include them as hidden fields:
<!-- in the outer loop: -->
<% =Html.HiddenFor(x => x.AllPages[i].DisplayName) %>
...
<!-- and then in the inner loop -->
<%= Html.HiddenFor(x => x.AllPages[i].actions[j].ActionName) %>
<%= Html.HiddenFor(x => x.AllPages[i].actions[j].DisplayName) %>
... and so on ...
我也强烈建议您使用编辑器模板,而不是在视图中编写这些循环。他们会自动为您的输入字段生成适当的名称,这样您就不必担心。关于这个话题,我有成千上万的答案。只是Google我的名字,并在搜索中添加编辑器模板asp.net mvc
,您应该会得到很多结果。
Also I would very strongly recommend you using editor templates instead of writing those loops in your views. They automatically will take care of generating proper names for your input fields so that you don't have to worry about. I have gazillions of answers on this topic. Just Google my name and add editor templates asp.net mvc
to your search and you should get many results.
这篇关于ASP.NET MVC中复杂类型的复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!