我有简单的测试模型:
public class MyModel
{
public InnerModel InnerModel { get; set; }
}
public class InnerModel
{
public int Value { get; set; }
}
在控制器中:
public ActionResult Index()
{
var model = new MyModel();
model.InnerModel = new InnerModel { Value = 3 };
return View("MyModelView", model);
}
我的模型视图:
@model MyModel
@{
var items = new List<SelectListItem>()
{
new SelectListItem {Text = "one", Value = "1"},
new SelectListItem {Text = "two", Value = "2"},
new SelectListItem {Text = "three", Value = "3"}
}; }
@Html.DropDownListFor(m=>m.InnerModel.Value,items,"no_selected")
当页面加载我看到选定的项目:
很好。
但是如果我添加 EditorTemplate InnerModel.cshtml:
@model InnerModel
@{
var items = new List<SelectListItem>()
{
new SelectListItem {Text = "one", Value = "1"},
new SelectListItem {Text = "two", Value = "2"},
new SelectListItem {Text = "three", Value = "3"}
}; }
@Html.DropDownListFor(m=>m.Value,items,"no_selected")
并更改MyModelView:
@model MyModel
@Html.EditorFor(m=>m.InnerModel,"InnerModel")
加载页面时,我看到:
为什么?MVC 错误?
更新: 这个真正的错误。看