cha*_*ara 4 asp.net ajax asp.net-mvc jquery asp.net-mvc-2
我正在尝试对控制器方法进行ajax调用.没有参数它工作正常.一旦我添加参数,我总是会收到一个空参数给cotroller.我想我已经正确地完成了传入ajax调用的参数.
<script type="text/javascript">
$(document).ready(function () {
$('#lstStock').change(function () {
var serviceURL = '<%= Url.Action("GetStockPrice", "Delivery") %>';
var dropDownID = $('select[id="lstStock"] option:selected').val();
alert(dropDownID); // here i get the correct selected ID
$.ajax({
type: "POST",
url: serviceURL,
data: '{"stockID":"' + dropDownID + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
alert(data.Result);
}
function errorFunc() {
alert('error');
}
})
});
</script>
Run Code Online (Sandbox Code Playgroud)
控制器:
[HttpGet]
public ActionResult GetStockPrice()
{
return View();
}
[HttpPost]
[ActionName("GetStockPrice")]
public ActionResult GetStockPrice1(string stockID)//I get the null parameter here
{
DeliveryRepository rep = new DeliveryRepository();
var value = rep.GetStockPrice(stockID);
return Json(new { Result = value }, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
von*_* v. 12
这是因为您将数据视为字符串
data: '{"stockID":"' + dropDownID + '"}',
Run Code Online (Sandbox Code Playgroud)
你可以把它改成:
data: { stockID: dropDownID },
Run Code Online (Sandbox Code Playgroud)
在某些情况下,根据控制器方法中声明的参数,您需要序列化数据.如果你需要这样做,那么你就是这样做的:
var o = { argName: some_value };
$.ajax({
// some other config goes here
data: JSON.stringify(o),
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18581 次 |
| 最近记录: |