我试图制定一个解决类似的缺乏在ASP.NET MVC中一个复选框组的。实现这个的典型方式是将具有相同的名称的复选框,每个值将其重新presents
I am trying to formulate a work-around for the lack of a "checkbox group" in ASP.NET MVC. The typical way to implement this is to have check boxes of the same name, each with the value it represents.
<input type="checkbox" name="n" value=1 /> <input type="checkbox" name="n" value=2 /> <input type="checkbox" name="n" value=3 />在提交时,它会逗号分隔的所有值的请求项目的n..所以请求[N] ==1,2,3如果提交时所有三个检查。在ASP.NET MVC中,你可以有n的参数作为一个数组来接受这个职位。
When submitted, it will comma delimit all values to the request item "n".. so Request["n"] == "1,2,3" if all three are checked when submitted. In ASP.NET MVC, you can have a parameter of n as an array to accept this post.
public ActionResult ActionName( int[] n ) { ... }所有上述工作正常。我的问题是,在验证失败时,该复选框都未恢复到自己的选中状态。任何建议。
All of the above works fine. The problem I have is that when validation fails, the check boxes are not restored to their checked state. Any suggestions.
问题code:(我开始与默认asp MVC项目)
Problem Code: (I started with the default asp mvc project)
控制器
public class HomeController : Controller { public ActionResult Index() { var t = getTestModel("First"); return View(t); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(TestModelView t) { if(String.IsNullOrEmpty( t.TextBoxValue)) ModelState.AddModelError("TextBoxValue", "TextBoxValue required."); var newView = getTestModel("Next"); return View(newView); } private TestModelView getTestModel(string prefix) { var t = new TestModelView(); t.Checkboxes = new List<CheckboxInfo>() { new CheckboxInfo(){Text = prefix + "1", Value="1", IsChecked=false}, new CheckboxInfo(){Text = prefix + "2", Value="2", IsChecked=false} }; return t; } } public class TestModelView { public string TextBoxValue { get; set; } public List<CheckboxInfo> Checkboxes { get; set; } } public class CheckboxInfo { public string Text { get; set; } public string Value { get; set; } public bool IsChecked { get; set; } } }ASPX
<% using( Html.BeginForm() ){ %> <p><%= Html.ValidationSummary() %></p> <p><%= Html.TextBox("TextBoxValue")%></p> <p><% int i = 0; foreach (var cb in Model.Checkboxes) { %> <input type="checkbox" name="Checkboxes[<%=i%>]" value="<%= Html.Encode(cb.Value) %>" <%=cb.IsChecked ? "checked=\"checked\"" : String.Empty %> /><%= Html.Encode(cb.Text)%><br /> <% i++; } %></p> <p><input type="submit" value="submit" /></p> <% } %>工作code
控制器
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(TestModelView t) { if(String.IsNullOrEmpty( t.TextBoxValue)) { ModelState.AddModelError("TextBoxValue", "TextBoxValue required."); return View(t); } var newView = getTestModel("Next"); return View(newView); }ASPX
int i = 0; foreach (var cb in Model.Checkboxes) { %> <input type="checkbox" name="Checkboxes[<%=i%>].IsChecked" <%=cb.IsChecked ? "checked=\"checked\"" : String.Empty %> value="true" /> <input type="hidden" name="Checkboxes[<%=i%>].IsChecked" value="false" /> <input type="hidden" name="Checkboxes[<%=i%>].Value" value="<%= cb.Value %>" /> <input type="hidden" name="Checkboxes[<%=i%>].Text" value="<%= cb.Text %>" /> <%= Html.Encode(cb.Text)%><br /> <% i++; } %></p> <p><input type="submit" value="submit" /></p>相似可以与HTML辅助来完成的东西当然,不过这工作。
Of course something similar could be done with Html Helpers, but this works.
推荐答案我不知道如何解决你的问题,但你可以用这个code定义复选框:
I don't know how to solve your problem, but you could define your checkboxes with this code:
<%= Html.CheckBox("n[0]") %><%= Html.Hidden("n[0]",false) %> <%= Html.CheckBox("n[1]") %><%= Html.Hidden("n[1]",false) %> <%= Html.CheckBox("n[2]") %><%= Html.Hidden("n[2]",false) %>需要
隐藏字段,因为如果没有被选中复选框,形式不发送任何价值。随着隐藏字段发送假的。
Hidden fields are needed, because if checkbox is not checked, form doesn't send any value. With hidden field it sends false.
您POST方法是:
[HttpPost] public ActionResult Test(bool[] n) { return View(); }这可能不是最佳的,我愿意接受意见,但它的工作原理:)
It may not be optimal and I am open to comments, but it works:)
修改
扩展版本:
<%= Html.CheckBox("n[0].Checked") %><%= Html.Hidden("n[0].Value",32) %><%= Html.Hidden("n[0].Checked",false) %> <%= Html.CheckBox("n[1].Checked") %><%= Html.Hidden("n[1].Value",55) %><%= Html.Hidden("n[1].Checked",false) %> <%= Html.CheckBox("n[2].Checked") %><%= Html.Hidden("n[2].Value",76) %><%= Html.Hidden("n[2].Checked",false) %>您POST方法是:
[HttpPost] public ActionResult Test(CheckedValue[] n) { return View(); } public class CheckedValue { public bool Checked { get; set; } public bool Value { get; set; } }我写的没有VS,所以它可能需要一点调整。
I wrote it without VS, so it may need little correction.
更多推荐
ASP.NET MVC复选框组
发布评论