views:

128

answers:

2

I've got something seemingly very simple not working.

I have got a model

public class Name: Entity
{
    [StringLength(10), Required]
    public virtual string Title { get; set; }
}

public class Customer: Entity
{
    public virtual Name Name { get; set; }
}

a view model

public class CustomerViweModel
{
    public Customer Customer { get; set; }
}

a view

       <% using(Html.BeginForm()) { %>
                    <%= Html.LabelFor(m => m.Customer.Name.Title)%>
                    <%= Html.TextBoxFor(m => m.Customer.Name.Title)%> 
                    <button type="submit">Submit</button>
        <% } %>

and a controller

[HttpPost]
public ActionResult Index([Bind(Prefix = "Customer")] Customer customer)
{
      if(ModelState.IsValid)
           Save
       else
           return View();
 }

No matter what I enter as the title (null, or a string > 10 chars), ModelState.IsValid is always true. The Title field in the Customer object has a value, so the data is being passed around, but not being validated?

Any clues?

+3  A: 

In your View I don't see any text box or a field allowing to send data to the controller, only a label. Properties will not be validated if they are not posted. Add a textbox, leave it blank and your model won't be valid any more:

<%= Html.TextBoxFor(m => m.Customer.Name.Title)%>

UPDATE:

Here's the code I've used:

Model:

public class Name
{
    [StringLength(10), Required]
    public virtual string Title { get; set; }
}

public class Customer
{
    public virtual Name Name { get; set; }
}

public class CustomerViewModel
{
    public Customer Customer { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index([Bind(Prefix = "Customer")]Customer cs)
    {
        return View(new CustomerViewModel
        {
            Customer = cs
        });
    }
}

View:

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

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using(Html.BeginForm()) { %>
        <%= Html.LabelFor(m => m.Customer.Name.Title)%>
        <%= Html.TextBoxFor(m => m.Customer.Name.Title)%> 
        <button type="submit">Submit</button>
    <% } %>
</asp:Content>

When you submit this form a validation error is shown.

Remark1: I've omitted the Entity base class in the models as I don't how does it look.

Remark2: I've renamed the variable in the Index action to cs. I remember that there was some problems with this in ASP.NET MVC 1.0 when you had the prefix and the variable named the same but I am not sure whether this applies here and I think it was fixed.

Darin Dimitrov
My bad, I do have a textbox in the view.
Jaimal Chohan
Well, I've just tried this example and it worked as expected, validation errors were shown for the title property.
Darin Dimitrov
A: 

Figured it out, it was becuase I'm referencing System.ComponentModel.DataAnnotations 3.6 instead of 3.5. From what I gather, 3.6 is for WCF RIA services only.

Jaimal Chohan