ajax调用C#方法不起作用

时间:2022-02-15 03:17:17

I want to call a method in C# from a client side AJAX/JQuery message. The client code is:

我想从客户端AJAX / JQuery消息中调用C#中的方法。客户端代码是:

        function TestClickFunc(userId) {

            $.ajax({
                url: "/Users/UpdateEmailDistributionListFlag",
                type: "POST",
                data: { "userId" :  userId },
                success: function (data) { alert(data); }
            });

        }

This method gets called with the correct parameter. However in my UsersController, this method does not get called;

使用正确的参数调用此方法。但是在我的UsersController中,此方法不会被调用;

   [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UpdateEmailDistributionListFlag(int userId)
    {
        // db update
        return View();
    }

Can you see why?

你能明白为什么吗?

2 个解决方案

#1


0  

Check if you have a route with parameter userId. In the default route the parameter's name is id, not userId so your method will not be found.

检查您是否有带参数userId的路由。在默认路由中,参数的名称是id,而不是userId,因此将找不到您的方法。

#2


0  

What I found was that the CDN was not working;

我发现CDN没有用;

I can't see the spelling mistake, but it worked when I used my local script!

我看不出拼写错误,但是当我使用我的本地脚本时它才有效!

#1


0  

Check if you have a route with parameter userId. In the default route the parameter's name is id, not userId so your method will not be found.

检查您是否有带参数userId的路由。在默认路由中,参数的名称是id,而不是userId,因此将找不到您的方法。

#2


0  

What I found was that the CDN was not working;

我发现CDN没有用;

I can't see the spelling mistake, but it worked when I used my local script!

我看不出拼写错误,但是当我使用我的本地脚本时它才有效!