Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Json Bind Bug?

I recently encountered an issue while implementing a JSON solution for a client. If the action parameter name matches a property name of the model, the binder fails to bind ANY properties.

An example:

Controller People

public ActionResult SetEmails(Person emails){
    ...
    return Content("");
}

Because the parameter name was called "emails" and that matches a property name of the Person model called "Emails".. the binder fails, but does not give any indication as to why..

They had a model called Person

public class Person {
    public string Name { get; set; }
    public List<string> Emails { get; set; }

    public Person() {
        Emails = new List<string>();
    }
}

Is this a bug, or a 'feature' ?

Just as a side note, my question is more in regards to how an arguments name could cause a conflict to a binder? The name shouldn't matter as it's the class type that defines it's schema to match against the json data.. why should the binder care what you name the argument or if it matches a property name within the class type of the argument itself?

like image 810
Mike Avatar asked Aug 30 '11 15:08

Mike


1 Answers

The problem is that you have an ambiguity in your dictionary, with two elements both named "emails" (though with different casing). DefaultModelBinder cannot resolve this ambiguity.

Two possible solutions: (1) if you are passing back an entire person model, change the name of your top-level element to "person" (which makes more sense, given the context), which would remove the ambiguity, and allow the binding to take place as expected, or (2) if you are just passing back a list of emails, change your action signature to public ActionResult SetEmails(List<Emails> emails).

I would not call what you have experienced either a bug or a feature, but an issue with your design.

like image 116
counsellorben Avatar answered Oct 04 '22 15:10

counsellorben