简体   繁体   English

从控制器传递字符串到视图

[英]passing string from controller to view

This JavaScript code is being used to pass a string from a view to an action in a controller: 此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 code in the controller: 控制器中的brandsOfACategory代码:

public ActionResult brandsOfACategory(string id)
    {
        return View();
    }

Code is not working as expected as id is coming as null. 由于id为null,因此代码无法正常工作。

Can someone please guide? 有人可以指导吗?

The code logic and syntax look fine. 代码逻辑和语法看起来不错。 To test: 去测试:

  1. On this page, run the code in your JS console. 在此页面上,在JS控制台中运行代码。
  2. Open the network tab. 打开网络标签。
  3. Open one of the links on this page in a new tab. 在新标签中打开此页面上的链接之一。
  4. The network request has a pending status. 网络请求的状态为待定。
  5. Dismiss the alert. 解除警报。
  6. The network request fires. 网络请求触发。

Steps 4 and 5 go away if you remove the alert. 如果删除警报,步骤4和5将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM