此代码用于使用Ajax和jQuery在视图事件中将锚元素的id
从视图发送到控制器。
<script type="text/javascript">
$(document).on('click', 'a', function () {
$.ajax({
type: 'POST',
url: '@Url.Action("/brandsOfACategory")',
contentType: 'application/json; charset:utf-8',
data: JSON.stringify(this.id)
})
});
</script>
用于捕获此字符串的操作方法是:
[HttpPost]
public ActionResult brandsOfACategory(string id)
{
return View();
}
但是此动作方法中的string id
为空。我检查了Chrome开发者工具的“网络”标签中的“请求有效载荷”,以检查参数的值,它是一个对象而不是字符串。屏幕上传到这里:https://imgur.com/a/XR3wDq5
答案 0 :(得分:1)
如果您尝试将ajax调用中的id
发送给控制器方法,则需要将其作为{key: value}
对来传递
所以代替这个
data: JSON.stringify(this.id)
尝试一下
data: JSON.stringify({id: this.id})