简体   繁体   中英

mvc call controller from jquery with parameter

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

您需要更改url ajax方法应如下所示:

url:'@Url.Action("Home", "singleValue")',

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM