簡體   English   中英

ASP.NET MVC如何找到哪個屬性出錯

[英]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] = ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM