0

I have filled object SearchHomeModel and want to pass this object to controller via ajax request. My controller is the following prototype:

public ActionResult DataTableUserList(SearchHomeModel search, int iDisplayStart, int iDisplayLength, string sEcho)

and I concat url string for it:

    url = "/User/DataTableUserList?SearchHomeModel.FirstName=" + Model.SearchKeys.FirstName + 
        "&SearchHomeModel.LastName=" + Model.SearchKeys.LastName + 
        "&SearchHomeModel.Title=" + Model.SearchKeys.Title + 
        "&SearchHomeModel.Company=" + Model.SearchKeys.Company;

(and then pass this url to ajax call)

when I see in debugger call DataTableUserList I see that iDisplayStart has value, but search is null. How to pass this object in my case? Thanks

John
  • 727
  • 2
  • 10
  • 17
  • Have a look at this [link][1] [1]: http://stackoverflow.com/questions/5980389/proper-way-to-use-ajax-post-in-jquery-to-pass-model-from-strongly-typed-mvc3-vie – Anwar Jan 14 '12 at 17:38
  • Also here http://stackoverflow.com/questions/4120212/mvc-ajax-json-post-to-controller-action-method – Paul Keister Jan 14 '12 at 17:49

3 Answers3

1

Why not Serialize the form and send to your action method ? If you have binded your model with the form elements, you will get a valid model with values filled there.

$.post("/User/DataTableUserList", $("form").serialize(), function (data) {

                     //do what you want with the response from your action method
});
Shyju
  • 214,206
  • 104
  • 411
  • 497
1

You might try something like...

$.ajax({
    url: '/User/DataTableUserList',
    data: {
        FirstName : '@Model.SearchKeys.FirstName',
        LastName : '@Model.SearchKeys.LastName',
        Title: '@Model.SearchKeys.Title',
        Company: '@Model.SearchKeys.Company'
    },
    success: function(result){
        // Do Something with Result
    }
});

Hopefully the MVC Model Binder will do the work for you this way.

jcreamer898
  • 8,109
  • 5
  • 41
  • 56
0

Try to do this instead:

url = "/User/DataTableUserList?search.FirstName=" + Model.SearchKeys.FirstName + 
        "&search.LastName=" + Model.SearchKeys.LastName + 
        "&search.Title=" + Model.SearchKeys.Title + 
        "&search.Company=" + Model.SearchKeys.Company;

I hope this helps :-)

Qorbani
  • 5,825
  • 2
  • 39
  • 47