tags:

views:

442

answers:

4

I understood that this Bind attribute was not necessary, but JSON type is not binding without it. What am I missing here? I am running RC1.

Edit:

this is supposed to work:

public JsonResult Index(Person person)
{
    do something with person.
}

But it won't work for some controller actions unless I do this:

public JsonResult Index([Bind(Prefix="")]Person person)
{

}

The first object is void.

jQuery Ajax:

  $.ajax({
               type: "POST",
               url: "/Index/Person",
               data: { PersonID: personID, Name: name },
               dataType: "json",
               success: function(data) {..}

       }
     }
  });
+2  A: 

The Bind attribute is not necessary for model binding in general. However, your question doesn't provide enough detail for anyone to make a judgment of what the actual problem is. If you're trying to post JSON to the server, then none of this will work. The default model binder only binds posted form values to arguments of the action method.

Haacked
see jquery above. It works with when I put the Bind attribute. Is this not recommended?
zsharp
+2  A: 

The code below works for me (download the entire sample project from here).

Also, just to check, are you sure you have at least the RC1 version of MVC? Early previews of MVC did require the Bind attribute, but that was changed in RC1 to make the [Bind] attribute unnessesary for common cases. See this post for details.

Javascript code:

$(function() {
    $("#result").text("Calling Ajax...");
    $.ajax({
        type: "POST",
        url: "/Home/Person",
        data: { Name: "Erv Walter", PersonID: "123" },
        dataType: "json",
        success: function(data) {
            $("#result").text(data.Name);
        }
    });
});

With this in the HTML:

<div id="result" />

Controller code:

[AcceptVerbs("POST")]
public JsonResult Person(Person person)
{
    person.Name = person.Name.ToUpper();
    return Json(person);
}

and the Person class looks like this:

public class Person
{
    public string Name { get; set; }
    public string PersonID { get; set; }
}
Erv Walter
+1  A: 

Do some sanity checking first. You probably have most, if not all of these, but without a little more code we're kind of in a bind:

Do you have the [AcceptVerbs(HttpVerbs.Post)] filter on your action? Try marking Person as [Serializable] too. What does your Person look like? Does it have a default constructor? Is there anything going on in the default constructor that might cause something to error out? Make sure you are using the exact same spelling on the properties on Person

Try making an action that doesn't use Person as an argument:

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Index(int PersonID, string Name) {}

Or take a FormCollection and call UpdateModel().

swilliams
i tries all this no success. Breaking out the variables as you did does work, but thats not what I want to do. My code is identical to the post above. the prob is only with JSonResult.
zsharp
i give you the credit for "sanity checking" advice, because i am an idiot.
zsharp
Man, I've been there. I think that if you don't come to that conclusion at least once a week, you're probably doing it wrong :).
swilliams
A: 

The problem was that the parameter variable name was the same name (but in lowercase) as a variable in the object.

zsharp