开发者

string does not pass to controller

开发者 https://www.devze.com 2023-02-21 21:58 出处:网络
i am trying to post my string called \'selected\' trough my $.ajax call but the controller(selected=null) receives a null value? selected has a开发者_如何学C value ({\'selected\':0100}) according to f

i am trying to post my string called 'selected' trough my $.ajax call but the controller(selected=null) receives a null value? selected has a开发者_如何学C value ({'selected':0100}) according to fiddler?

[HttpPost]
public ActionResult Index(string selected)
{
    return Json(new {value = "this is a test"});
}

$(document).ready(
     $("#btnSave").click(
         function () {
         var checkboxesselected = "0100";

         $.ajax({ type: 'POST',
             url: "/Home/Index",
             datatype: 'json',
             data: "{'selected':" + checkboxesselected + "}"
         });
     }
)


The problem is you're sending the data to jQuery as a string literal as opposed to an object. Your line with the data parameters should be data: {selected: checkboxesselected }


You're missing the quotes around "checkboxesselected "

$(document).ready(
    $("#btnSave").click(
        function () {
        var checkboxesselected = "0100";

        $.ajax({ type: 'POST',
            url: "/Home/Index",
            datatype: 'json',
            data: "{ 'selected' : '" + checkboxesselected + "'}"
        });
    }
)


try:

var val = { selected: checkboxesselected };

and then: ...

datatype: 'json',
data: val

...

0

精彩评论

暂无评论...
验证码 换一张
取 消