I have a model like this:
public PurchaseOrder
{
[Required] [StringLength(15)]
public virtual string OrderNumber {get;set;}
// etc.
}
Are you sure that the error is coming from the OrderNumber field?
It seems strange that the error message has Order Number in 2 words.
Try changing the name of the field or removing the required tag to make sure that this is where the error is coming from.
Well I have "solved" it, but I do not really understand why the changes I made have helped.
I had to do three things:
Remove the json filter (filters don't bind)
Change the contentType to application/json
$.ajaxSetup({ contentType: "application/json; charset=utf-8" });
Use the MVC futures download Microsoft.Mvc.dll as described here: http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx. Where is says to add this to Application_Start() in Global.asax.cs:
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
Now I dont know exactly why that has worked, but it has.
Unfortunately it has had a negative side effect: the contenttype is applied to all $.get() and $.post() methods, and broken all my jqgrids - they only seem to work if the content type is the default of application/x-www-form-urlencoded
So I've asked 2 follow on questions:
Is it possible to set the content type in a $.post() call? Then I wouldn't need to set it globally Jquery - How to make $.post() use contentType=application/json?
Is it possible to make jqrid work if the contenttype is application/json? Jquery - How to make $.post() use contentType=application/json?
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.