开发者

MVC3 client server validation message mismatch

开发者 https://www.devze.com 2023-02-28 22:54 出处:网络
I\'m getting different error messages for the same error depending on where the validation occurs.The kicker is that the server is generating both messages so it\'s not jquery related.

I'm getting different error messages for the same error depending on where the validation occurs. The kicker is that the server is generating both messages so it's not jquery related.

Using the following model:

public class MyModel
{
    public int MyNumber { get; set; }
}

With client validation and an invalid value the error message is:

The field MyNumber must be a number.开发者_JAVA百科

With server validation the error message is:

The value 'x' is not valid for MyNumber.

With javascript disabled on the client the server actually generates both messages but displays only the server one:

<input class="input-validation-error text-box single-line" data-val="true" data-val-number="The field MyNumber must be a number." data-val-required="The MyNumber field is required." id="MyNumber" name="MyNumber" type="text" value="x" />
<span class="field-validation-error" data-valmsg-for="MyNumber" data-valmsg-replace="true">The value &#39;x&#39; is not valid for MyNumber.</span>

My questions are:

  1. Why are they different?
  2. How do I change them?

Edit: found a partial explanation and hack here: http://forums.asp.net/p/1512140/3608427.aspx


I'm not sure why they're different. But to customize the error message for both client and server side validation, you can use Data Annotations. There's a tutorial on Data Annotations on ASP.NET.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号