<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Blog posts by Lars-Woetmann-Pedersen</title><link href="http://world.optimizely.com" /><updated>2013-07-05T17:15:26.0000000Z</updated><id>https://world.optimizely.com/blogs/Lars-Woetmann-Pedersen/</id> <generator uri="http://world.optimizely.com" version="2.0">Optimizely World</generator> <entry><title>MVC ValidationAttribute ErrorMessage populated at runtime</title><link href="https://world.optimizely.com/blogs/Lars-Woetmann-Pedersen/Dates/2013/7/MVC-ValidationAttribute-ErrorMessage-populated-at-runtime/" /><id>&lt;p&gt;I am working on a project using EPiServer 7 and MVC 4. One of the nice features in MVC is ValidationAttributes in the model such as RequiredAttribute and RegularExpressionAttribute. These attributes give you server side validation on your model, if combined with jquery.validate and jquery.validate.unobtrusive you also get client side validation without writing any javascript yourself. This is very nice and keeps the code clean and it looks something like this:&lt;/p&gt;  &lt;p&gt;In my controller I create a viewmodel and pass it to the view, using a viewmodel is optional but I like it and follow the pattern described by Joel Abrehamsson here: http://joelabrahamsson.com/episerver-and-mvc-what-is-the-view-model/&lt;/p&gt;  &lt;div class=&quot;csharpcode&quot;&gt;   &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; EducationPlanSearchController : ProtectedPageControllerBase&amp;lt;EducationPlanSearchPage&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;{&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    [HttpGet]&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; ActionResult Index(EducationPlanSearchPage currentPage)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        EducationPlanSearchPageViewModel viewModel = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; EducationPlanSearchPageViewModel(currentPage);&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; View(viewModel);&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    ... HttpPost part left &lt;span class=&quot;kwrd&quot;&gt;out&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;My viewmodel has one property its required and has to have a correct format:&lt;/p&gt;



&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; EducationPlanSearchPageViewModel : PageViewModel&amp;lt;EducationPlanSearchPage&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;{&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; EducationPlanSearchPageViewModel(EducationPlanSearchPage currentPage) : &lt;span class=&quot;kwrd&quot;&gt;base&lt;/span&gt;(currentPage)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    [Required(ErrorMessage = &lt;span class=&quot;str&quot;&gt;&amp;quot;Compiletime error required&amp;quot;&lt;/span&gt;)]&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    [RegularExpression(&lt;span class=&quot;str&quot;&gt;@&amp;quot;^\d{6,6}-?\d{4,4}$&amp;quot;&lt;/span&gt;, ErrorMessage = &lt;span class=&quot;str&quot;&gt;&amp;quot;Compiletime error format&amp;quot;&lt;/span&gt;)]&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Cpr { get; set; }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The view is very simple it is just a form with one input field and a submit button:&lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;@model Pdk.Website.Models.ViewModels.EducationPlanSearchPageViewModel&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;lt;div &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt;=&lt;span class=&quot;str&quot;&gt;&amp;quot;content-block&amp;quot;&lt;/span&gt;&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &amp;lt;div &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt;=&lt;span class=&quot;str&quot;&gt;&amp;quot;formLine&amp;quot;&lt;/span&gt;&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        @&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; (Html.BeginForm())&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            @Html.ValidationSummary()&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            @Html.EditorFor(m =&amp;gt; m.Cpr)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            &amp;lt;input type=&lt;span class=&quot;str&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt;=&lt;span class=&quot;str&quot;&gt;&amp;quot;@Model.CurrentPage.SearchButtonText&amp;quot;&lt;/span&gt;/&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;In my layout I include the needed jquery files:&lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;lt;script src=&lt;span class=&quot;str&quot;&gt;&amp;quot;/Scripts/jquery-1.10.1.js&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;lt;script src=&lt;span class=&quot;str&quot;&gt;&amp;quot;/Scripts/jquery.validate.js&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;lt;script src=&lt;span class=&quot;str&quot;&gt;&amp;quot;/Scripts/jquery.validate.unobtrusive.js&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;If someone press the submit button with an empty input field or one with the wrong format they will get an error message, and this happens client side, using the ErrorMessage defined in the attribute for the property in the viewmodel:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://farm4.staticflickr.com/3689/9213250221_e3f723ff14.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is all standard MVC so far, there is just one problem with this because I would like to set the ErrorMessage on the ValidationAttribute runtime using an EPiServer page property. That would enable the editors to change the error message when they want to.&lt;/p&gt;

&lt;p&gt;To do this I have to implement my own&amp;#160; model metadata provider which is used by&amp;#160; ASP.NET MVC to load the validation attributes, tell my IoC container to use this, and set the ErrorMessage when the viewmodel is created. First I create my own&amp;#160; model metadata provider:&lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MyModelMetaDataProvider : CachedDataAnnotationsModelMetadataProvider&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;{&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;readonly&lt;/span&gt; Dictionary&amp;lt;Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;, List&amp;lt;Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt; _errorMessageDictionary = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Dictionary&amp;lt;Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;, List&amp;lt;Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt;();&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; SetValidationErrorMessage(Type containerType, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; propertyName, Type validationAttribute, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; errorMessage)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (!&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(errorMessage))&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt; key = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;(containerType, propertyName);&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (!_errorMessageDictionary.ContainsKey(key))&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;                _errorMessageDictionary[key] = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt; &lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt; = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;(validationAttribute, errorMessage);&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            _errorMessageDictionary[key].Add(&lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt;);                &lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable&amp;lt;Attribute&amp;gt; attributes, Type containerType, Type modelType, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; propertyName)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        CachedDataAnnotationsModelMetadata model = &lt;span class=&quot;kwrd&quot;&gt;base&lt;/span&gt;.CreateMetadataPrototype(attributes, containerType, modelType, propertyName);&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt; key = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;(containerType, propertyName);&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (_errorMessageDictionary.ContainsKey(key))&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            List&amp;lt;Tuple&amp;lt;Type, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;&amp;gt; errorMessageList = _errorMessageDictionary[key];&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            &lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (ValidationAttribute attribute &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; attributes.OfType&amp;lt;ValidationAttribute&amp;gt;())&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;                var &lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt; = errorMessageList.FirstOrDefault(t =&amp;gt; t.Item1 == attribute.GetType());&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;                &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt; != &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;                {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;                    attribute.ErrorMessage = &lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt;.Item2;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;                }                    &lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;            }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; model;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;It contains a dictionary where I add the error messages I want to update, the key in the dictonary is the viewmodel type and property name, the value is the attribute type and error message. When a view with a model containing validation attributes is created&amp;#160; CreateMetadataPrototype is called and the ErrorMessages is set using this dictionary.&lt;/p&gt;

&lt;p&gt;The viewmodel is updated to call SetValidationErrorMessage from the constructor, I get the new error messages from the PageData model, thus allowing the editors to edit it as a property on the page:&lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; EducationPlanSearchPageViewModel : PageViewModel&amp;lt;EducationPlanSearchPage&amp;gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;{&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; EducationPlanSearchPageViewModel(EducationPlanSearchPage currentPage) : &lt;span class=&quot;kwrd&quot;&gt;base&lt;/span&gt;(currentPage)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    {&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        MyModelMetaDataProvider metadataProvider = (MyModelMetaDataProvider)ModelMetadataProviders.Current;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        metadataProvider.SetValidationErrorMessage(GetType(), &lt;span class=&quot;str&quot;&gt;&amp;quot;Cpr&amp;quot;&lt;/span&gt;, &lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(RequiredAttribute), currentPage.ErrorCprEmpty);&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;        metadataProvider.SetValidationErrorMessage(GetType(), &lt;span class=&quot;str&quot;&gt;&amp;quot;Cpr&amp;quot;&lt;/span&gt;, &lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(RegularExpressionAttribute), currentPage.ErrorCprFormatWrong);&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;#160;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    [Required]&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    [RegularExpression(&lt;span class=&quot;str&quot;&gt;@&amp;quot;^\d{6,6}-?\d{4,4}$&amp;quot;&lt;/span&gt;)]&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Cpr { get; set; }&lt;/code&gt;&lt;/pre&gt;

  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Before it works I have to tell my IoC container to use my own ModelMetadataProvider:&lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;container.For&amp;lt;ModelMetadataProvider&amp;gt;().Use&amp;lt;MyModelMetaDataProvider&amp;gt;();&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Now when the Index action on EducationPlanSearchController is called the viewmodel is created and in its constructor the correct ErrorMessage is set on the current ModelMetadataProvider which is of the type MyModelMetaDataProvider and when the view is called the method CreateMetadataPrototype is called, and the correct ErrorMessage is set.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://farm8.staticflickr.com/7363/9213250205_af013c9036.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I hope this can help others in the same situation, I realize the code could use some more comments so don’t hesitate to ask if there is any problems with it. And if anybody has a cleaner solution to this problem, (apart from convincing the editor to give up runtime editing of error messages) then I would like the see it.&lt;/p&gt;</id><updated>2013-07-05T17:15:26.0000000Z</updated><summary type="html">Blog post</summary></entry></feed>