您需要指定集合中项目的索引。
这是控制器代码:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return
View(new List<BaseModel>() { new BarModel() { BaseProp = "Bar" }, new FooModel() { BaseProp = "Foo" } });
}
[HttpPost]
public ActionResult Index(IList<BaseModel> model)
{
return this.View(model);
}
}
如您所见,它没有什么特别之处。神奇之处在于:
@using MvcApplication1.Models
@model IList<MvcApplication1.Models.BaseModel>
@{
ViewBag.Title = "title";
//Layout = "_Layout";
}
<h2>title</h2>
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
@Html.EditorFor(p => p[i])
}
<input type="submit" value="Save" />
}
如您所见,传递给 EditorFor 的表达式包含集合中当前项目的索引。这里解释了为什么需要这样做。简而言之,EditorFor 为 name 属性包含集合中项目索引的每个属性返回一个输入元素,例如
<input class="text-box single-line" name="[0].BaseProp" type="text" value="Bar" />
更新
如果您试图保留对象的类型,您将需要在模型中具有一个特殊的属性,该属性将存储特定的模型类型和一个自定义IModelBinder
实现,它将基于该属性创建特定的模型实例。下面是模型类。该Type
属性将呈现为隐藏输入:
namespace MvcApplication1.Models
{
using System.Web.Mvc;
public class BaseModel
{
public string BaseProp { get; set; }
[HiddenInput(DisplayValue = false)]
public virtual string Type
{
get
{
return _type ?? this.GetType().FullName;
}
set
{
_type = value;
}
}
private string _type;
}
public class FooModel : BaseModel
{
public string FooProp { get; set; }
}
public class BarModel :BaseModel
{
public string BarProp { get; set; }
}
}
这是自定义模型绑定器的示例实现:
public class BaseModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// call to get the BaseModel data so we can access the Type property
var obj = base.BindModel(controllerContext, bindingContext);
var bm = obj as BaseModel;
if(bm != null)
{
//call base.BindModel again but this time with a new
// binding context based on the spefiic model type
obj = base.BindModel(
controllerContext,
new ModelBindingContext(bindingContext)
{
ModelMetadata =
ModelMetadataProviders.Current.GetMetadataForType(null, Type.GetType(bm.Type)),
ModelName = bindingContext.ModelName
});
}
return obj;
}
}
您需要在 application_start 上注册自定义活页夹:
ModelBinders.Binders.Add(typeof(BaseModel), new BaseModelBinder());