我的困难在于如何完成“如何做部件?”这一部分。
namespace MvcApplication1.Models
{
public class Test { public Test()
{
Name = "Test";
Subs = new List<Sub>();
Subs.Add(new Sub { Num = 1, SubName = "A", Valid=false });
Subs.Add(new Sub { Num = 2, SubName = "B" , Valid= true}); }
public string Name { get; set; }
public List<Sub> Subs { get; set; }
}
public class Sub {
public int Num { get; set; }
public string SubName { get; set; }
public bool Valid { get; set; }
}
}
查看:
@model MvcApplication1.Models.Test
@{
View.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@{
var i = 0;
}
}
<h2>Test2</h2>
@using (Html.BeginForm())
{
<p>Test Name</p>@Html.EditorFor(m => m.Name)
foreach(var item in Model.Subs)
<p>Sub 1</p>
@Html.EditorFor(m => item[i].Num)<------
@Html.EditorFor(m => item[i].SubName**)<------How to do this part?**
@Html.EditorFor(m => item[i++].Valid**)
<input type="submit" value="Save" />
}
控制器:
public ActionResult Index()
{
Test test = new Test();
return View(test);
}
[HttpPost]
public ActionResult Index(Test test)
{
return View(test);
}
}
答案 0 :(得分:0)
您可以自定义编辑器模板:
@using (Html.BeginForm())
{
<p>Test Name</p>
@Html.EditorFor(m => m.Name)
@Html.EditorFor(m => m.Subs)
<input type="submit" value="Save" />
}
并在相应的编辑器模板(~/Views/Home/EditorTemplates/Sub.cshtml
)内:
@model MvcApplication1.Models.Sub
@Html.EditorFor(m => m.Num)
@Html.EditorFor(m => m.SubName)
@Html.EditorFor(m => m.Valid)