Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 2 Model encapsulated within ViewModel Validation
    primarykey
    data
    text
    <p>I am trying to get validation to work in ASP.NET MVC 2, but without much success.</p> <p>I have a complex class containing a large number of fields. (Don't ask - this is oneo f those real-world situations best practices can't touch) This would normally be my Model and is a LINQ-to-SQL generated class. </p> <p>Because this is generated code, I have created a MetaData class as per <a href="http://davidhayden.com/blog/dave/archive/2009/08/10/AspNetMvc20BuddyClassesMetadataType.aspx" rel="nofollow noreferrer">http://davidhayden.com/blog/dave/archive/2009/08/10/AspNetMvc20BuddyClassesMetadataType.aspx</a>.</p> <pre><code>public class ConsultantRegistrationMetadata { [DisplayName("Title")] [Required(ErrorMessage = "Title is required")] [StringLength(10, ErrorMessage = "Title cannot contain more than 10 characters")] string Title { get; set; } [Required(ErrorMessage = "Forename(s) is required")] [StringLength(128, ErrorMessage = "Forename(s) cannot contain more than 128 characters")] [DisplayName("Forename(s)")] string Forenames { get; set; } // ... </code></pre> <p>I've attached this to the partial class of my generated class:</p> <pre><code>[MetadataType(typeof(ConsultantRegistrationMetadata))] public partial class ConsultantRegistration { // ... </code></pre> <p>Because my form is complex, it has a number of dependencies, such as SelectLists, etc. which I have encapsulated in a ViewModel pattern - and included the ConsultantRegistration model as a property:</p> <pre><code>public class ConsultantRegistrationFormViewModel { public Data.ConsultantRegistration ConsultantRegistration { get; private set; } public SelectList Titles { get; private set; } public SelectList Countries { get; private set; } // ... </code></pre> <p>So it is essentially ViewModel=>Model</p> <p>My View then has:</p> <pre><code> &lt;p&gt; &lt;%: Html.LabelFor(model =&gt; model.ConsultantRegistration.Title) %&gt; &lt;%: Html.DropDownListFor(model =&gt; model.ConsultantRegistration.Title, Model.Titles,"(select a Title)") %&gt; &lt;%: Html.ValidationMessage("Title","*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;%: Html.LabelFor(model =&gt; model.ConsultantRegistration.Forenames) %&gt; &lt;%: Html.TextBoxFor(model =&gt; model.ConsultantRegistration.Forenames) %&gt; &lt;%: Html.ValidationMessageFor(model=&gt;model.ConsultantRegistration.Forenames) %&gt; &lt;/p&gt; </code></pre> <p>The problem is, the validation attributes on the metadata class are having no effect. I tried doing it via an Interface, but also no effect. I'm beginning to think that the reason is because I am encapsulating my model within a ViewModel.</p> <p>My Controller (Create Action) is as follows:</p> <pre><code>[HttpPost] public ActionResult Create(Data.ConsultantRegistration consultantRegistration) { if (ModelState.IsValid) // this is always true - which is wrong!! { try { consultantRegistration = ConsultantRegistrationRepository.SaveConsultantRegistration(consultantRegistration); return RedirectToAction("Edit", new { id = consultantRegistration.ID, sectionIndex = 2 }); } catch (Exception ex) { ModelState.AddModelError("CreateException",ex); } } return View(new ConsultantRegistrationFormViewModel(consultantRegistration)); } </code></pre> <p>As outlined in the comment, the ModelState.IsValid property always returns true, despite fields with the Validaiton annotations not being valid. (Forenames being a key example).</p> <p>Am I missing something obvious - considering I am an MVC newbie? I'm after the mechanism demoed by Jon Galloway at <a href="http://www.asp.net/learn/mvc-videos/video-10082.aspx" rel="nofollow noreferrer">http://www.asp.net/learn/mvc-videos/video-10082.aspx</a>.</p> <p>(Am aware t is similar to <a href="https://stackoverflow.com/questions/1260562/asp-net-mvc-model-viewmodel-validation">ASP.NET MVC Model/ViewModel Validation</a> but that post seems to talk about xVal. I have no idea what that is and suspect it is for MVC 1)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload