How to delete MVC2 verification messages?

I am using ASP.NET C # MVC2 and I have the following field in the model with the following data annotation validation attributes:

[DisplayName("My Custom Field")]
[Range(long.MinValue, long.MaxValue, ErrorMessage = "The stated My Custom Field value is invalid!")]
public long? MyCustomField{ get; set; }

In the form, this field should allow the user to leave it blank and display a verification message if the user tries to enter a value that cannot be expressed as a number. From a verification point of view, this works as intended and displays the following error messages:

The specified value of My Custom Field is invalid!

My Custom Field must be a number.

The first verification message is a special verification message that I wrote, and the second verification message is the one generated by MVC2. I need to get rid of the second because of its redundancy. How should I do it? In my opinion, I have the following markup

<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm())
   { %>
   <%:Html.ValidationSummary(false)%>
   <% Html.ValidateFor(m => m.MyCustomField); %>
+3
1

, , , . RangeAttribute.

string RangeAttribute, , .

, :

 [DisplayName("My Custom Field")]
 [MyCustomRangeAttribute(/* blah */)] //<-- the new range attribute you write
 public string MyCustomFieldString
 {
   get; set;
 }

 public int? MyCustomField
 {
   get 
   { 
     if(string.IsNullOrWhiteSpace(MyCustomField))
       return null;
     int result;
     if(int.TryParse(MyCustomField, out result))
       return result;
     return null;
   }    
   set
   {
      MyCustomFieldString = value != null ? value.Value.ToString() : null;
   }
 }

int? , - string.

[Bind(Exclude"MyCustomField")] - MVC int?. internal. -, -.

- ModelState.Errors , ...

+2

All Articles