2

I have a form and the only item that is required is the customer name. So in my model, I have:

 [DisplayName("Customer name*:")]
 [Required]
 public string CustomerName
 { get; set; }

Previously, I was doing an HTML post and everything worked fine including validation.

Now, I've "ajaxified" the form, using Ext.direct.mvc (http://code.google.com/p/ext-direct-mvc/), which is a significant fact, and posting the data in Json format and the data is getting posted successfully.

enter image description here

When I put a breakpoint in my code (currently modified for debugging purposes):

 [DirectInclude]
    [HttpPost]
    public ActionResult SaveOrUpdateOrderLines(CustomerOrderModel customerOrderModel)
    {

        if (!ModelState.IsValid)
        {
            return ModelState.JsonValidation();
        }

        return null;

I see that the CustomerOrderModel.CustomerOrderHeader.CustomerName = ""

enter image description here

But the ModelState.IsValid is true.

enter image description here

Now for some of the things I've tried:

  1. I have inserted the following code, just before checking for ModelState.isValid, in order to ensure that CustomerName = null

    customerOrderModel.CustomerOrderHeader.CustomerName = null;

  2. I have tried using TryUpdateModel(customerOrderModel) but I get the following error message:

    TryUpdateModel threw an exception of type 'System.MissingMethodException'

  3. I've tried modifying the json data so that the "root" "CustomerOrderHeader" was renamed to "customerOrderModel" to match the parameter.

None of these things worked. So what could I be doing wrong that validation isn't working anymore? What steps can I take to debug the issue?

EDIT for counsellorBen

enter image description here

EDIT 2 for counsellorben

enter image description here

DavidS
  • 2,179
  • 4
  • 26
  • 44
  • Ok I've found the answer which is detailed here http://stackoverflow.com/questions/4465432/asp-net-mvc-2-controllers-tryvalidate-doesnt-validate-the-list-items-within/4489226#comment10011080_4489226. – DavidS Nov 17 '11 at 14:15

1 Answers1

0

The problem is that when trying to bind a Json response, the name of the variable in your controller action must match the name of the variable passed on the client side. Your model is valid, because CustomerOrderHeader is null.

In your client script, you should wrap your entire model in an element named "customerOrderModel", so the name matches the variable name in your action.

counsellorben
  • 10,924
  • 3
  • 40
  • 38
  • I've added an Edit section in the OP containing an image. Is that what you mean? Because if so, that still doesn't work. – DavidS Aug 17 '11 at 13:03
  • D'oh! I looked at the Json being returned, and missed that it is wrapped in a "data" element. Either (1) remove the data wrapper, or (2) remove the "customerOrderModel" element, and rename your variable in your action to "data". – counsellorben Aug 17 '11 at 13:36
  • I've followed advice 2 as you can see in the edit. I've updated my server side code but still that doesn't work. – DavidS Aug 17 '11 at 13:50
  • Also I might have made myself clear but I believe, from putting breakpoints on the code, that the data is getting bound to the model correctly. The issue is that the model state is not getting updated. – DavidS Aug 17 '11 at 14:54
  • I've just done a cut down version of what I would like to achieve and having the entire model in an element named "customerOrderModel" actually "breaks" things. It's actually expecting "data". I ran the example that can be found here http://code.google.com/p/ext-direct-mvc/downloads/list and also used Fiddler to change the request manually. It is indeed very strange. Moreover, upon further research, I really need the TryModelUpdate although it's throwing an exception. – DavidS Aug 18 '11 at 11:37
  • I have to apologise to you but I didn't realise that the fact that I was using Ext.Direct would be significant, as I've found from some research into the topic. I'll update the OP. – DavidS Aug 18 '11 at 18:30
  • I'm now no longer using Ext.Direct and you assertion that "the name of the variable in your controller action must match the name of the variable passed on the client side" is not true in my case. The names are not matching but it still works. – DavidS Aug 23 '11 at 07:24