我试图为ASP.NET MVC中缺少"复选框组"制定解决方法.实现此目的的典型方法是使用相同名称的复选框,每个复选框都带有它所代表的值.
<input type="checkbox" name="n" value=1 />
<input type="checkbox" name="n" value=2 />
<input type="checkbox" name="n" value=3 />
Run Code Online (Sandbox Code Playgroud)
提交时,它将逗号分隔所有值到请求项"n"..所以请求["n"] =="1,2,3"如果提交时都检查了所有三个.在ASP.NET MVC中,您可以将参数n作为数组来接受此帖子.
public ActionResult ActionName( int[] n ) { ... }
Run Code Online (Sandbox Code Playgroud)
所有上述工作都很好. 我遇到的问题是,当验证失败时,复选框不会恢复到其检查状态.有什么建议.
问题代码:(我从默认的asp.net mvc项目开始)
调节器
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(); …
Run Code Online (Sandbox Code Playgroud) 使用webforms的一个主要原因是易于维护viewstate.我想构建一个asp.net mvc应用程序,以便我有什么选择来维护viewstate?
亲切的问候