3

My current code looks like

<!-- some html -->
{
    // some code
    @Html.Partial("~/Views/AdminUser/Main.cshtml", Model.AdminUserModel)
}

however, i need this to instead be an ajax call. How do I do an jquery ajax call where the model is included in the call?

3
  • What you could do is make an ajax call with a url query. Set the variables based on your query string. Commented Aug 15, 2013 at 15:32
  • problem with that solution is the model could be huge. mainly need to know how to pass the model. Commented Aug 15, 2013 at 16:35
  • was thinking you could possibly make the ajax call include a json (stringify?) version of the model and on the back-end put the model back into the object? not quite sure how. Commented Aug 15, 2013 at 16:37

1 Answer 1

3

How I do it is an ajax call passing the id:

$.ajax({
    url: "@(Url.Action("Action", "Controller", new { id = "----" }))/".replace("----", id),
    type: "POST",
    cache: false,
    async: true,
    success: function (result) {
         $(".Class").html(result);
    }
});

and then in your controller have the action set up like

public PartialViewResult Action(string id)
{
     //Build your model
     return PartialView("_PartialName", model);
}

if you do need to pass a model to the controller through ajax, if you create a jquery object that has the same fields as the model and stringify and pass it, it will come through correctly.

var toSend = {};
toSend.ID = id;
toSend.Name = name; 

etc, then in the ajax call

data: JSON.stringify(toSend),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.