ASPNET MVC - Why is ModelState.IsValid false “The x field is required” when that field does have a value?

后端 未结 3 1958
孤城傲影
孤城傲影 2021-01-12 06:45

I have a model like this:

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


        
3条回答
  •  时光说笑
    2021-01-12 07:35

    There is a chance that your jQuery post is not posting in the right way back to the controller. You may be posting just the order number rather than an object that accurately represents the model. I'd have to see your javascript to be absolutely certain.

    The correct way to post to that method would be (assuming your order number is stored in a javascript variable called "orderNumber":

    $.post([url to method], 
           { order: { OrderNumber: orderNumber } }, 
           function(json) {
           //Insert code for actions to take after AJAX post returns.
           },
           'json');
    

    If this is the case, then the next step I'd take is to double check that the javascript is indeed capturing the value of your order number and posting it as part of the request. A quick examination through Firebug should be able to tell you if that's happening or not.

提交回复
热议问题