asp.net-mvcvalidationxval

ASP.NET MVC xVal with Strongly Typed ViewModel


I can't get xVal validation to work with strongly typed viewmodels.

Every method in xVal seems to want a prefix which is not used when dealing with strongly typed viewmodels.

My view contains code similar to this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ContactForm>" %>
<%= Html.TextBox("firstName", Model.FirstName) %>

And the following code goes in the controller:

try
{
    var theModel = form.ToModel();
    _contactRepository.Save(theModel);
}
catch (RulesException ex)
{
    ex.AddModelStateErrors(ModelState, string.Empty); // Passing string.Empty for prefix, since I don't use prefixes.
}

return View(form);

However, the above code doesn't work. I've surely missed something, but don't know what. This is my first time using xVal.

Thankful for any help!


Solution

  • I think the problem is in the fact that you don't use prefixes. If you debug the ModelState you can see validations for things like ".FirstName" although they should be like "FirstName". Because of that the client side validation summaries and stuff doesn't show those validation error messages.

    I think this is a bug in xVal.