我想将值(1)传递给我的控制器,如您所见:
SELECT RETAILER_ID, COUNT(RETAILER_ID) AS TOTAL
FROM ret_retailer
LEFT JOIN temp_sg_screen_sto_72_mos_summary ON ret_retailer.RETAILER_FULL_NAME = temp_sg_screen_sto_72_mos_summary.RETAILER_NAME
WHERE MAY_2018 < 100000
GROUP BY RETAILER_ID
脚本:
<input id="myButton2" type="button" value="Call Controller Method" onclick="myff('1');" />
这是我的控制器
function myff(re) {
$.getJSON('@Url.Action("MyControllerMethod","Reception",new { area ="Admin"})', function (data) {
refid = re;
});
}
但是当我点击按钮时,操作被触发但我的操作中的public JsonResult MyControllerMethod(string refid) {
return Json("Controller Method call", JsonRequestBehavior.AllowGet);
}
为空为什么?
答案 0 :(得分:3)
而不是$.getJSON()
,请尝试以下代码:
function myff(re) {
$.ajax({
dataType: "json",
url: '@Url.Action("MyControllerMethod","Reception",new { area ="Admin"})',
data: {refid : re}
});
注意:虽然$.getJSON()
内部调用$.ajax()
,但后者会为您提供更多灵活性 - http://api.jquery.com/jQuery.getJSON/
答案 1 :(得分:2)
您需要传递$.getJSON()
var url = '@Url.Action("MyControllerMethod","Reception",new { area ="Admin"})';
$.getJSON(url, { refid: re }, function (data) {
console.log(data); // returns "Controller Method call"
});
我还建议你使用Unobtrusive Javascript而不是用行为污染标记
<input id="myButton2" type="button" data-x="1" value="Call Controller Method" />
$('#myButton2.click(function) {
var re = $(this).data('x');
$.getJSON(.....
});