views:

1034

answers:

3

The ability to let the model handle its own validation has lead me to begin playing with the MVC 2 preview release. So far, I like the simplicity of the validation scheme. However, I have run into a roadblock. This validation style works fine for simple view model objects. For example if I have a model object named car and I'm looking to create a view to create a new car:

-----Model-------

public class Car
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Color { get; set; }
}

-----Controller---------

public class CarController : Controller
{
    public ActionResult Create()
    {
        Car myCar = new Car();
        return View("Create", myCar);

    }

    [HttpPost]
    public ActionResult Create(Car myCar)
    {
        if (!ModelState.IsValid)
        {
            return View("Create", myCar);
        }

        //Do something on success
        return View("Index");

    }

}

-------View--------------

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Car>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>

    <% 
        using (Html.BeginForm()) {%>

        <fieldset>
            <legend>Edit User Profile</legend>
            <p>
                <label for="Id">Id:</label>
                <%= Html.TextBox("Id", Model.Id)%>
                <%= Html.ValidationMessage("Id") %>
            </p>
            <p>
                <label for="Name">Name:</label>
                <%= Html.TextBox("Name", Model.Name)%>
                <%= Html.ValidationMessage("Name") %>
            </p>
            <p>
                <label for="Color">Color:</label>
                <%= Html.TextBox("Color", Model.Color)%>
                <%= Html.ValidationMessage("Color") %>
            </p>

            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

</asp:Content>

This works like a charm. But not all of my views, or model objects for that matter, are simple. I might have a car model object like:

-----Model-------

public class PaintScheme
{
    public int Red { get; set; }
    public int Blue { get; set; }
    public int Green { get; set; }
}

public class Car
{
    public string Id { get; set; }
    public string Name { get; set; }
    public PaintScheme Paint{ get; set; }
}

-------View--------------

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Car>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>

    <% 
        using (Html.BeginForm()) {%>

        <fieldset>
            <legend>Edit User Profile</legend>
            <p>
                <label for="Id">Id:</label>
                <%= Html.TextBox("Id", Model.Id)%>
                <%= Html.ValidationMessage("Id") %>
            </p>
            <p>
                <label for="Name">Name:</label>
                <%= Html.TextBox("Name", Model.Name)%>
                <%= Html.ValidationMessage("Name") %>
            </p>
            <p>
                <label for="Red">Color Red:</label>
                <%= Html.TextBox("Red", Model.Paint.Red)%>
                <%= Html.ValidationMessage("Red") %>
            </p>
            <p>
                <label for="Blue">Color Blue:</label>
                <%= Html.TextBox("Blue", Model.Paint.Blue)%>
                <%= Html.ValidationMessage("Blue") %>
            </p>
            <p>
                <label for="Green">Color Green:</label>
                <%= Html.TextBox("Green", Model.Paint.Green)%>
                <%= Html.ValidationMessage("Green") %>
            </p>

            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

</asp:Content>

When I add the PaintScheme properties to my view, they are not carried over with the "myCar" object passed into my controller action. Is there a way to resolve this without having to rebuild the object from a form collection and then checking the ModelState?

A: 
  1. You should have the public setter for properties you want to be bound. I wonder how the first sample works for you as everything is private.
  2. You have to post at least one value of PaintScheme property to be able to bind it.
  3. All child properties should be prefixed with the path to it. Where path can be defined as *(PropertyName.)**.

It seems the point 3 is not satisfied in the View. Change the appropriate part of the view to this:

        <p>
            <label for="Red">Color Red:</label>
            <%= Html.TextBox("Paint.Red")%>
            <%= Html.ValidationMessage("Red") %>
        </p>
        <p>
            <label for="Blue">Color Blue:</label>
            <%= Html.TextBox("Paint.Blue")%>
            <%= Html.ValidationMessage("Blue") %>
        </p>
        <p>
            <label for="Green">Color Green:</label>
            <%= Html.TextBox("Paint.Green")%>
            <%= Html.ValidationMessage("Green") %>
        </p>

Additionally note that I removed explicit values from the TextBox helper to avoid possible NullReferenceException.

Dmytrii Nagirniak
He left those out for brevity.
mxmissile
Ok. I think I see what's wrong. Updated the answer accordingly.
Dmytrii Nagirniak
That was exactly what I was missing. Thanks Dmitriy.
gun_shy
A: 

For the color part you can have something like this, being an int I dont think you will use a textbox but this will bind your red color (if the input value is a number)

 <p>
                <label for="Red">Color:</label>
                <%= Html.TextBox("Red", Model.Paint.Red)%>
                <%= Html.ValidationMessage("Red") %>
 </p>
San
+1  A: 

The easiest way to deal with this is to flatten your model using a dto. Then use automapper to map your domain object to your view model. This translation could be defined in such a way that you convert the enum to a string and back. Then validation will work and your bom won't touch the view maintaining your seperation of concerns fairly well.

Andrew Siemer
Why to flatten and introduce another dependency instead of just *correctly* using the feature (ModelBinding)? The model shown is not any complex to split it.
Dmytrii Nagirniak
Maintaining a seperation of concerns from layer to layer will almost always make things cleaner.
Andrew Siemer