0

I have a variable in Jquery which I want to pass to one of the parameters in my Controller method.

This is my AJAX code:

<script>
    $(document).on("click", ".getDetails", function (e) {
        var val = $(this).val();
        alert(val);
        $.ajax({
            url: '/Home/GetDetails',
            contentType: 'application/html; charset=utf-8',
            type: 'POST',
            data: {
                partsId: val
            },
            dataType: 'html'
        })
            .success(function (result) {
                $('#detailsPlace').html(result);
            })
            .error(function (xhr, status) {
                alert(status);
            });
    })
</script>

Here is my controller code:

public ActionResult GetDetails(int partsId)
{
    return PartialView();
}

All I want to do is pass the val variable to my partsId parameter, the code that I am currently using keeps returning the partsId parameter as null whenever I debug it.

1
  • Just remove contentType: 'application/html; charset=utf-8', Commented Nov 6, 2016 at 21:04

1 Answer 1

1

why dont use json format?

$(document).on("click", ".getDetails", function (e) {
    var val = $(this).val();
    alert(val);
    $.ajax({
        url: '/Home/GetDetails',
        contentType: 'application/json',
        type: 'POST',
        data: JSON.stringify({"partsId": val}),
        dataType: 'json'
    })
        .success(function (result) {
            $('#detailsPlace').html(result);
        })
        .error(function (xhr, status) {
            alert(status);
        });
})
Sign up to request clarification or add additional context in comments.

8 Comments

I need to return a partial view so I cant can I? I just tried it and it hit the error not the success
which partial you want to return ?
my controller is returning a html partial view?
see this for returning partial : stackoverflow.com/a/2539577/6350942
why do I need to do that when that part is working all I want to do is pass the variable over?
|

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.