string does not pass to controller
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
...
精彩评论