ASPNET MVC - Why is ModelState.IsValid false "The x field is required" when that field does have a v

Posted by JK on Stack Overflow See other posts from Stack Overflow or by JK
Published on 2010-05-08T01:38:41Z Indexed on 2010/05/08 1:48 UTC
Read the original article Hit count: 1453

Filed under:
|
|
|

I have a model like this:

public PurchaseOrder 
{
    [Required] [StringLength(15)]
    public virtual string OrderNumber {get;set;}
    // etc.        
}

When I submit an order from the view (using $.post, not input type=submit) it goes to my controller class:

public class PurchaseOrderController
{
    public JsonResult Save(PurchaseOrder order)
    {
        // TryUpdateModel(order); // commented out since modelstate.isvalid remains false anyway
        if (ModelState.IsValid)
        {
            // its never valid 
        }
    }
}

ModelState.IsValid always returns false, with the error: "The Order Number field is required." But there is a value in this field (?? why)

Why would it say "value is required" when it does have a value? Have I missed something? Is it because of the $.post instead of the submit? What can I do?

This is what the debugger looks like:

alt text

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc