ModelState always valid

Posted by Jaimal Chohan on Stack Overflow See other posts from Stack Overflow or by Jaimal Chohan
Published on 2010-04-17T16:34:31Z Indexed on 2010/04/17 16:43 UTC
Read the original article Hit count: 327

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)%>
                    <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?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc-2