ASP.NET MVC Model Error Localization Context

First of all, I have to say that I understand how Data Annotation-based model validation works in ASP.NET MVC4, and I have successfully implemented it using DataAnnotationsModelValidatorProvider . Therefore, I do not need help with the setup.

But when it comes to HtmlHelpers , I struggle to understand the context of the error message. And by saying context , I mean what kind of error we're talking about. Which attribute returned the error?

What I can get is the Key for the error and the current ErrorMessage , but programmatically, there is nothing that, at least, I know, will bind the error that we are talking about. Whether it is a Required attribute or some other attribute, there is no way to determine how to distinguish them.

Release the script a bit. I have a custom HtmlHelpers for rendering ContentEditable elements. For example, Html.ContentEditableValidationMessageFor(m => m.firstName); . It will output something like this:

 <span contenteditable="true" data-valmsg-for="firstName" data-valmsg-replace="Please provide first name" class="field-validation-error">Please provide first name</span> 

Now I have a jQuery plugin for processing and saving changes to the ContentEditable element, and it will be saved in the backend. However, there is nothing in the user interface that could tell which error message we are talking about. People can easily see this RequiredAttribute , but programmatically there is no data to distinguish it from some MinLengthAttribute for example.

In this case, if I just used data-valmsg-for="firstName" as the key for localization, this will return the same error message for all errors related to the same property.

To deploy it

What would be the best practice when a ModelState is available to emit a unique identifier for ModelError? Given that I am using ASP.NET MVC4 and DataAnnotationsModelValidatorProvider .

I can think of many ways to “hack it together”, but I would like to use ModelState and everything that MVC provides. If it all comes down to writing a custom ModelValidatorProvider , then I'm all open to it. So far, this is the best and most sustainable way to get around this. I’m all for doing more now and less later than hacking it now and hacking it forever so that it works

+4
source share
2 answers

Can you give some context around the need to know which rule caused the validation error, can this be the reason that you are trying to do something that you also do not need?

In general, I use FluentValidation ( http://fluentvalidation.codeplex.com/wikipage?title=mvc ) instead of checking data annotations for many reasons, de-cluttering models, checking unit testing logic, allowing for a much more complex test involving business the logic. If you use third-party libraries, I would look at them as if they always solved any validation problems that I had in the past.

It allows you to write C # code that deals with your model validation through a free API. This one has the MVC extension, which works everything for you so as not to create a model validation class, it has little effect. An example of your code snippet above would be ...

 RuleFor(modelname => modelname.FirstName).NotEmpty().WithMessage("lease provide first name"); 
+4
source

Even the implementation of ModelValidatorProvider will not help, it is just a mechanism for providing ModelValidator based on model metadata. When the result is called during the model binding process in the ModelValidator controller action, it is simply ModelValidationResult , which contains only MemberName and the text Message .

I think there is a dirty way to figure out which ModelValidator failed by checking the error message as follows:

 var modelErrors = ModelState.Where(m => m.Value.Errors.Count > 0).Select(m => new { Name=m.Key , Errors=m.Value.Errors}); 

By checking ErrorMessage of Errors for each key in the modelErrors files on ValidatorProvider messages, you can find out which error belongs to which validator.

+2
source

All Articles