There are times where you may want to display custom validation error messages in MVC. As for me, I’m not a huge fan of the classic ValidationSummary HTML helper. I will still annotate my data model, but I want the errors to have a better presentation in the UI. Occasionally, I may want to display one error at a time. In order to do all of this, there’s a few steps that need to be implemented.
Often times you may want to render a custom validation message that contains HTML markup. If you simply add the line:
This will encode the markup so that less than signs and greater than signs are rendered < and >, respectively. This, of course, is not what you want.
To render your HTML markup, its a little dirty, but it does work.
Simply add the following to your view:
Where “Email” is a property of your model.
When deploying an MVC application to a hosting environment you may receive the error:
[FileNotFoundException: Could not load file or assembly ‘Microsoft.Web.Infrastructure, Version=126.96.36.199, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.]
This is also one of a few component libraries that are needed for deploying an MVC application:
The system libraries are installed with .NET 4, however, ‘Microsoft.Web.Infrastructure.dll’ is only installed when Visual Studio is installed on the machine. Therefore, short of needing to install MVC and Visual Studio on a production environment, we need to deploy the libraries with out application – and we’d like to do so automatically.
There are a few ways to automatically deploy the ‘Microsoft.Web.Infrastructure.dll’ component library with your application. The steps depend on which version of Visual Studio you are using.
Subscribe to receive new posts in your inbox.