views:

524

answers:

4

I am finally experimenting and trying to learn MVC after years of asp.net.
I am used to using asp.net AJAX PageMethods where you can pass an object that automagically gets parsed to whatever type the parameter is in that method.

Javascript:

PageMethods.AddPerson({First:"John",Last:"Doe"});

Code-Behind:

[WebMethod]
public static Result AddPerson(Person objPerson)
{
    return Person.Save();
}

How would do this using MVC and jQuery?
Did just have to send strings and parse the json to object?

+1  A: 

That depends on how complex your form data is going to be. Let's use a jQuery example:

$.ajax({
    url: '\Persons\AddPerson', // PersonsController, AddPerson Action
    data: { First: "John", Last: "Doe" },
    type: 'POST',
    success: function(data, status)
    {
        alert('Method called successfully!');
    }
});

So we post two pieces of data. If the Person class has two properties called 'First' and 'Last', then the default ASP.NET MVC Model Binder should have no problems putting this form data into those properties (everything else will be defaulted).

Of course, you could always make a custom model binder for the Person type, and then you can take whatever form values you want and put them into any property at all, or call some other kind of logic.

Tejs
what about methods that have more than one parameter?
Kenneth J
For each parameter, MVC will attempt to bind any form data to either the parameter itself (if it is a simple type), or to any properties on that parameter (if it is a complex type). So if you have two parameters, each with a First property, then both should get the value specified in the "First" key posted to the server. There is also a specific naming syntax you can use if you wish to bind it to only one complex type.
Tejs
I am having weird results where this works in FF but IE in the code-behind the method parameter gets an object with all the fields initialized to null?
Kenneth J
When you breakpoint your code, check the Request.Form collection to see what keys have been posted. Between IE and FF, it's probably different.
Tejs
A: 

When you POST a form via ajax to an action method on your controller, the ModelBinder architecture kicks in to parse the posted form values into business objects for you. You can leverage modelbinding in a few different ways.

public ActionResult MyAction(MyObject obj)
{
}

In the above example, the modelbinder implicitly tries to create a MyObject from the information it received in the request.

public ActionResult MyAction(FormCollection stuff)
{
   MyObject obj = new MyObject();
   TryUpdateModel(obj);
}

Here we are explicitly trying to bind the posted form data to an object that we created. The ModelBinder will try to match the posted values to the properties of the object.

In either of these cases, you can query the ModelState object to see if there were any errors that occurred during translation of the posted values to the object.

For an introduction to model binding, see here.

For advanced modelbinding to lists and dictionaries, see Phil Haack's post.

womp
A: 

You could do something like this:

 var person = {};
 person["First"] = $("#FirstName").val();
 person["Last"] = $("#LastName").val();

 $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "/Admin/AddPerson",
      data: JSON.stringify(person),
      dataType: "json",
      success: function(result) {

      },
      error: function(result) {

      }
 });

and then on your admin controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddRelease(Person p)
{
    // Code to add person            

}

The JSON.stringify method is available from here. You could also use a model rather than the Person object as a parameter that way you could handle all of your validation.

jaltiere
the $.ajax method allows you to handle this asynchronously.
jaltiere
+1  A: 

I have a post that covers AJAX calls to ASP.NET MVC action methods. It covers the following combinations:

  • HTTP GET, POST
  • jQuery methods $.get, $.getJSON, $.post
  • Sending parameters to the action methods
  • Returning parameters (strings and JSON) from the action methods
  • Posting form data
  • Loading a MVC partial view via AJAX

AJAX calls to ASP.NET MVC action methods using jQuery

rcravens