1

I want to call MVC controller method from Jquery. Below is my code but it is not working

Controller:

    public ActionResult singleValue(string valuetoset)
    {
        //Code
    }

JQuery:

$('#User').live('change', function (e) {
    var userValue = e.target.options[e.target.selectedIndex].value;

 $.ajax({
        url: "/Home/singleValue",
        type: 'GET',
         data: { valuetoset: userGuideValue },
        success: function (result) {
            alert(result);
        },
        error: function () {
            alert("error");
        }
    });
});

I need to pass valuetoset argument, but it always going as null

2 Answers 2

2

You need to change url ajax method should be like this:

url:'@Url.Action("Home", "singleValue")',
Sign up to request clarification or add additional context in comments.

Comments

0

Your data parameter needs to be in JSON format. There are a number of ways to do so, here's one:

var myData = {};
myData["valuetoset"] = userValue;

Here, valuetoset corresponds to your controller action parameter. Then, your data declaration should be as follows:

data: myData

Or, you could just include userValue in your URL if your routes are set up correctly since this is a GET action:

url: host + "/Home/singleValue/" + userValue

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.