2

I have the following JSon call that I want to call a method in Controller & accepts JSon object to update Partial View:

<script type="text/javascript">
    function DoAjaxUpdate(btnClicked) {
        btnClicked = $(btnClicked);
        var $form = btnClicked.parents('form');
        var url = '@Url.Action("Remove")';


        $.getJSON(
            url,
            $form.serialize(),
        function () {                
            if (data.htmlPartialView != null) {
                return $("#divPartialView").load(obj.htmlPartialView);
            }
        });

        return false;
    }
</script>

Unfortunately, this isn't passing the data properly and instead appends it to the URL as a query string such as: http://www.myLink.com/MyController/Remove?dataID=1359&dataMember=1

Please help. Thanks

1
  • Check this answer Commented May 9, 2013 at 0:02

3 Answers 3

1

That's what happens with $("form").serialize(). It will serialize everything on the form and put it on the query string.

As long as your Remove action method takes an instance of the model that is on your original view, then it will be transformed using the values in the query string.

To send it as JSON, you'd have to use JSON.stringify():

JSON.stringify($form.serialize())
Sign up to request clarification or add additional context in comments.

3 Comments

I updated $form.serialize() to JSON.stringify($form.serialize()) but still seems to have the same result. Do I need any other update? Am I missing something?
@Dak Hmm it must always be the case then, is there any harm in it being the query string?
I'm getting a 404 error because there is no view named "Remove"? I just want this function to call ActionMethod "Remove" & refresh the partial view inside "divPartialView" with updated data.
0

The callback need parameter,like this:

    $.getJSON(
        url,
        $form.serialize(),
        function (obj) {                
            if (obj.htmlPartialView) {
                $("#divPartialView").load(obj.htmlPartialView);
        }
    });

by the way,if (obj.htmlPartialView) is same to if (data.htmlPartialView != null)

Comments

0

You are calling getJSON which sends the data as a GET request which uses the querystring. If you want to use the POST method, use (from the jQuery site):

$.post(url, data, function(data) {
});

or

$.ajax({
    type: "POST",
    url: url,
    data: data,
    success: success,
    dataType: dataType
});

More information: http://api.jquery.com/jQuery.post/

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.