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

4 posts, 0 answers
  1. Linus
    Linus avatar
    18 posts
    Member since:
    Jul 2008

    Posted 01 Apr 2013 Link to this post

    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:

    - 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.

  2. Ken
    Ken avatar
    4 posts
    Member since:
    Sep 2012

    Posted 02 Apr 2013 Link to this post

    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?
  3. Linus
    Linus avatar
    18 posts
    Member since:
    Jul 2008

    Posted 02 Apr 2013 Link to this post

    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();
            return Request.CreateResponse(HttpStatusCode.OK);
            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 :)
  4. Vladimir Iliev
    Vladimir Iliev avatar
    2194 posts

    Posted 03 Apr 2013 Link to this post

    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!
Back to Top