This is a migrated thread and some comments may be shown as answers.

Model binding to complex object not working when updating (ModelState.IsValid == false)

3 Answers 437 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Linus
Top achievements
Rank 2
Linus asked on 01 Apr 2013, 11:16 PM
Been looking hard for answers to this but haven't been successful. These are the conditions:
  • MVC 4
  • Kendo MVC 2013,1,319,340
  • Grid using Ajax Binding
  • Using Web Api
I'm binding the grid to a model with a structure that looks something like this:

Organization
- Id
- Name
- Address
---- Id
---- Street
---- Zip
---- City

Most works fine (Creating (POST), Deleting (DELETE), Get (get)). However, when trying to update (PUT), the Address is not properly bound in the controller. I've attached a screenshot from fiddler showing the request body. Looks kinda weird to me.

I've also attached two screens showing the invalid ModelState and the error.

So, my question is if it's possible to properly bind the whole object (Organization) including the Address? I quess my model could be flattened but if possible I'd like to avoid that to keep it cleaner.

3 Answers, 1 is accepted

Sort by
0
Ken
Top achievements
Rank 1
answered on 02 Apr 2013, 11:20 AM
I have had success with what you are trying to acheive (except for when using DateTime).

Do you mind showing what you're doing with your controller?
0
Linus
Top achievements
Rank 2
answered on 02 Apr 2013, 07:36 PM
Not doing anything weird really, the method signature captures the PUT action and looks like this (simplified):
public HttpResponseMessage Put(int id, Organization org)
{
    if (ModelState.IsValid && (id == org.Id))
    {
        OrganizationRepository rep = new OrganizationRepository();
        rep.Update(org);
 
        return Request.CreateResponse(HttpStatusCode.OK);
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }
}
The organization properties looks correct except for the nested Address object. It looks like the Address objects propeties is getting bound with "list"-values. E.g. all the string-properties like Address.City are getting bound to the value: "(Collection)". When looking at Fiddler, the body contains both Address.City and Address[City]... Maybe I'm missing some grid and/or datasource configuration?

It would be great if you had a working example to post :)
0
Vladimir Iliev
Telerik team
answered on 03 Apr 2013, 09:16 AM
Hi Linus,

 
I would suggest to check the "Editing custom editor" demo in the KendoUI for ASP.NET MVC offline examples which demonstrates exact same scenario as yours. Also please make sure that both the 
Adress property in the Organization object and the properties of the Adress object have get and set methods. 

Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Linus
Top achievements
Rank 2
Answers by
Ken
Top achievements
Rank 1
Linus
Top achievements
Rank 2
Vladimir Iliev
Telerik team
Share this question
or