列表与类型描述符的模型绑定
给定以下派生类型:
public class Base {
public string Id {get;set;}
}
public class Contact : Base {
public string FirstName {get;set;}
public string LastName {get;set;
}
public class Organization : Base {
public string Name {get;set;}
}
我想使用自定义模型绑定程序来绑定类似的内容:
[HttpPost]
public ActionResult UpdateMultiple(List<Base> items) {
for each (var item in items) {
if (item.GetType().Equals(typeof(Contact)) {
// update
} else if (item.GetType().Equals(typeof(Organization)) {
// update
}
}
return RedirectToAction("index");
}
我的计划是每个项目都将有一个自定义类型描述符:
<input type="hidden" name="items[0].EntityType" value="MyNamespace.Contact, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<input type="text" name="items[0].FirstName" />
<input type="text" name="items[0].LastName" />
<input type="hidden" name="items[1].EntityType" value="MyNamespace.Organization, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<input type="text" name="items[1].Name" />
我已经为单个(非集合)对象开发了一个自定义模型绑定程序:
public class EntityTypeModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var typeValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".EntityType");
var type = Type.GetType((string)typeValue.ConvertTo(typeof(string)),true);
var model = Activator.CreateInstance(type);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
return model;
}
}
但是有人对如何转换此模型活页夹以处理集合有任何建议吗?我不知所措。
致以最诚挚的问候并感谢您的回复。
哈尔
Given the following derived types:
public class Base {
public string Id {get;set;}
}
public class Contact : Base {
public string FirstName {get;set;}
public string LastName {get;set;
}
public class Organization : Base {
public string Name {get;set;}
}
I would like to bind something like this using a custom model binder:
[HttpPost]
public ActionResult UpdateMultiple(List<Base> items) {
for each (var item in items) {
if (item.GetType().Equals(typeof(Contact)) {
// update
} else if (item.GetType().Equals(typeof(Organization)) {
// update
}
}
return RedirectToAction("index");
}
My plan is that each item will have a custom type descriptor:
<input type="hidden" name="items[0].EntityType" value="MyNamespace.Contact, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<input type="text" name="items[0].FirstName" />
<input type="text" name="items[0].LastName" />
<input type="hidden" name="items[1].EntityType" value="MyNamespace.Organization, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<input type="text" name="items[1].Name" />
I've developed a custom model binder for a single (non-collection) object:
public class EntityTypeModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var typeValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".EntityType");
var type = Type.GetType((string)typeValue.ConvertTo(typeof(string)),true);
var model = Activator.CreateInstance(type);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
return model;
}
}
But does anyone have any suggestions on how I could convert this model binder to handle a collection? I'm at a loss.
Best regards and thanks for the response.
Hal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所展示的这个模型活页夹已经可以处理集合。您所需要做的就是将以下行放入
Application_Start
中:它足够智能,并且也可以与集合一起使用,因为没有为
List
注册模型绑定程序> 它将是被调用的默认模型绑定器。它检测到您有一个集合,并为该集合的每个元素调用相应的模型绑定器。因为您已经为Base
类型注册了模型绑定器,所以将自动使用自定义绑定器。This model binder you have shown already handles collections. All you need is to put the following line in your
Application_Start
:It is intelligent enough and it will work with collections as well because there is not registered model binder for
List<Base>
it will be the default model binder that will be invoked. It detects that you have a collection and invokes the corresponding model binder for each element of the collection. And because you have registered a model binder for theBase
type you custom binder will be automatically used.