![](/img/trans.png)
[英]How can i resolve this error the 'connectionStringName' attribute is not allowed in web.config of asp.net in MVC 4
[英]ASP.NET MVC How can I find which attribute has been error
我想在ModelBinder中出現錯誤,在ModelState中自動附加錯誤代碼。 所以我已經擴展了默認屬性以添加屬性。(關於RequiredAttribute,RangeAttribute等):
//new Attribute
public class TestRequiredAttribute : RequiredAttribute
{
public int ErrorCode { get; set; }
}
但是我不知道,如何知道ModelBinder中哪個屬性出錯?
public class TestModelBinder : DefaultModelBinder
{
protected override void SetProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
System.ComponentModel.PropertyDescriptor propertyDescriptor,
object value)
{
//the error was happend in here
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
//in here to find which attribute has been error?
to find attribute and to get ErrorCode then
bindingContext.ModelState[modelStateName+code] = ...
}
}
您可以到MVC的來源
http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/DefaultModelBinder.cs
並重寫DefaultModelBinder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.