尝试这个:
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var myViewModel = new MyViewModel()
{
Form = new MyFormViewModel {Name = "Header"},
FormViewModels = new List<MyFormViewModel>()
{
new MyFormViewModel() {Name = "First"},
new MyFormViewModel() {Name = "Secound"}
}
};
return View(model: myViewModel);
}
[HttpPost]
public ActionResult Index(FormCollection collection, List<MyFormViewModel> FormViewModels, MyViewModel myView)
{
return View();
}
}
楷模:
public class MyViewModel
{
public MyFormViewModel Form { get; set; }
public IList<MyFormViewModel> FormViewModels { get; set; }
}
public class MyFormViewModel
{
public string Name { get; set; }
}
看法:
@model MvcChat.Models.MyViewModel
@{
ViewBag.Title = "Home Page";
}
@using(Html.BeginForm())
{
@Html.EditorFor(model=>model.Form.Name)
for (int i = 0; i < Model.FormViewModels.Count; i++)
{
@Html.EditorFor(x => x.FormViewModels[i])
}
<input type="submit" value="go"/>
}
结果应该是这样的(http://i.stack.imgur.com/t0Je0.png):