想要通过强类型传递Radio的值到控制器,在视图中不能使用foreach的方式取得各个Radio项,要使用for的形式,代码如下:
Models:
public class Fo { public string foName { get; set; } public List<Models.a> aList { get; set; } } public class a { public string name { get; set; } public int age { get; set; } public int option { get; set; } public List<Option> optionList { get; set; } } public class Option { public string option { get; set; } public int score { get; set; } public Option() { } public Option(string o, int s) { this.option = o; this.score = s; } }
Controllers:
public ActionResult Index() { Models.Fo fo = new Models.Fo(); fo.foName = "xxx"; fo.aList = new List<Models.a>(); for (int i = 0; i < 3; i++) { Models.a model = new Models.a(); model.name = "name" + i.ToString(); model.age = 30 + i; model.optionList = new List<Models.Option>(); model.optionList.Add(new Models.Option("是", 10)); model.optionList.Add(new Models.Option("否", 20)); fo.aList.Add(model); } return View(fo); } public string Regist(Models.Fo model) { return "ok"; }
Views:
@model MvcApplication1.Models.Fo @using (Html.BeginForm("Regist", "Home", FormMethod.Post)) { <p>@Html.TextBoxFor(m => m.foName)</p> for (int i = 0; i < Model.aList.Count; i++) { <p> @Html.TextBoxFor(m => m.aList[i].name) @for (int l = 0; l < Model.aList[i].optionList.Count; l++ ) { <span> @Html.RadioButtonFor(m => Model.aList[i].option,Model.aList[i].optionList[l].score) @Model.aList[i].optionList[l].option </span> } </p> } <input type="submit" id="btnsubmit" value="提交" class="submit" /> }
L‘ 2015/11/17 15:52
.NET MVC Form Radio 通过强类型向控制器传值
原文:http://www.cnblogs.com/Velo/p/4971956.html