【发布时间】:2019-01-02 05:58:41
【问题描述】:
此 JavaScript 代码用于将字符串从视图传递到控制器中的操作:
<script type="text/javascript">
$(document).on('click', 'a', function () {
$.ajax({
type: 'POST',
url: '/brandsOfACategory',
contentType: 'application/json; charset:utf-8',
data: JSON.stringify(this.id)
})
});
</script>
控制器中的brandsOfACategory代码:
public ActionResult brandsOfACategory(string id)
{
return View();
}
代码未按预期工作,因为 id 为 null。
有人可以指导吗?
【问题讨论】:
-
this.id可能包含 null - 也无需使用contentType,因为您可以像data: { id: "someid" }一样传递它。
标签: javascript jquery html asp.net asp.net-mvc