ASP.NET MVC:多模型绑定
是否可以使用某种 Multibinders,就像这样?
[Authorize]
[AcceptVerbs("POST")]
public ActionResult Edit([CustomBinder]MyObject obj)
{
///Do sth.
}
当我也配置了这样的默认活页夹时:
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
}
我想要的是拥有 DataAnnotationsBinder (它验证字符串长度、正则表达式等数据)以及另外设置字段值的自定义活页夹的好处。
我不能为此只编写 1 个活页夹,因为我使用 EntitiyFramework 并与 DataAnnotations 结合使用,它会产生如下结构:
[MetadataType(typeof(MyObjectMetaData))]
public partial class MyObject
{
}
public class MyObjectMetaData
{
[Required]
[StringLength(5)]
public object Storename { get; set; }
}
is it possible to use some kind of Multibinders, like this?
[Authorize]
[AcceptVerbs("POST")]
public ActionResult Edit([CustomBinder]MyObject obj)
{
///Do sth.
}
When i ALSO have configured a default binder like this:
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
}
What i want is to have the benefits of the DataAnnotationsBinder ( which validates the data for stringlength, regexps, etc ) and additionally my custom binder which sets the field values.
I can't write only 1 binder for this, as iam using the EntitiyFramework and in combination with the DataAnnotations it results in contstruct like this:
[MetadataType(typeof(MyObjectMetaData))]
public partial class MyObject
{
}
public class MyObjectMetaData
{
[Required]
[StringLength(5)]
public object Storename { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试在自定义模型绑定器中调用默认模型绑定器。
You can try calling the default model binder in your custom model binder.
为什么不直接继承 DataAnnotationsModelBinder 呢?
Why don't you just inherit from DataAnnotationsModelBinder?