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

2

3 Answers 3

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
});
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your variant, but it's not appropriate for my concrete case (i have to pass something more data than form)
also, page can be without <form>
Then you can pass items via querystring to the server
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.

1 Comment

why it could be inside search object?
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 :-)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.