带有参数的调用操作方法 MVC 4 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17702427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:16:16  来源:igfitidea点击:

call action method with parameters MVC 4 javascript

javascriptasp.net-mvc-4redirect

提问by dansv130

I'm tired and stupid, but heres my coding problem:

我又累又笨,但这是我的编码问题:

We are using d3.js to draw on a google map element in the MVC4 action method called Live. We have implemented on click for the d3.js element and need to redirect from the javascript to another MVC action method.

我们正在使用 d3.js 在名为 Live 的 MVC4 操作方法中绘制一个谷歌地图元素。我们已经为 d3.js 元素实现了点击,并且需要从 javascript 重定向到另一个 MVC 操作方法。

the action method "declaration" looks like this:

动作方法“声明”如下所示:

public ActionResult Visualization(String appId = "", String userId = "")

In javascript we have inside the d3.js function a code snippet that works and looks like this:

在 javascript 中,我们在 d3.js 函数中有一个代码片段,其工作方式如下所示:

.on("click", function (d, i) {
        // just as an example use for the click-event. 
        alert(d.AppName); }

Where dhas AppId, and UserIdaswell.

其中dAppIdUserId

What we now want to do is to create a redirect for the click event, calling the action method along with parameters from d. This means when you click the d3.js element you would be redirected to the other page with pre-set parameters.

我们现在要做的是为点击事件创建一个重定向,调用 action 方法以及来自d 的参数。这意味着当您单击 d3.js 元素时,您将被重定向到具有预设参数的另一个页面。

We have tried things like:

我们尝试过类似的事情:

window.location.href = "/StatsLocationController/Visualization/appId=" + d.AppIdentifier + "/userId=" + d.DeviceId

We also tried to use window.location.replace(), but none of it has worked, I guess we haven't figured out the correct syntax for the actionLink, but have a hard time finding examples when googling it. We are thankful for any help we can get!

我们还尝试使用 window.location.replace(),但都没有奏效,我想我们还没有找到 actionLink 的正确语法,但是在谷歌搜索时很难找到示例。我们非常感谢我们能得到的任何帮助!

采纳答案by dansv130

I forgot I left this without accepting an answer.

我忘了我没有接受答案就离开了。

simonlchilds have figured out the problem, that we included the Controllerin the call.

simonlchilds 已经解决了问题,我们将 包含Controller在调用中。

So the final call looked like:

所以最后的调用看起来像:

window.location.href = "/StatsLocation/Visualization/appId=" + d.AppIdentifier + "/userId=" + d.DeviceId

where StatsLocationis the name of the controller.

StatsLocation控制器的名称在哪里。

Such a silly mistake, but at least it had a simple solution and maybe it can help someone.

这么愚蠢的错误,但至少它有一个简单的解决方案,也许它可以帮助某人。

回答by simonlchilds

Instead of:

代替:

window.location.href = Html.ActionLink("Visualization", "StatsLocationController", new{ appId=d.AppId, userId=d.UserId}, new{})

Try:

尝试:

window.location.href = '@Url.Action("Visualization", "StatsLocationController", new{ appId=d.AppId, userId=d.UserId})'

This is assuming you have your routing set up appropriately?

这是假设您正确设置了路由?

Edit:

编辑:

The sort of routing that would make this work is something like:

使这项工作的路由类型类似于:

routes.MapRoute(
    "MyRoute", 
    "{controller}/{action}/{appId}/{userId}", 
    new {
         controller = "Home", 
         action = "Index", 
         appId = UrlParameter.Optional, 
         userId = UrlParameter.Optional 
    });

Second Edit

第二次编辑

I've just noticed that you're calling your controller StatsLocationController- Unless your C# class is called StatsLocationControllerController- this is wrong. Asp.Net MVC assumes that for a controller, there will be a class called StatsLocationControllerand so you should reference your controllers without the Controllerpart. So in this example your controller should be called StatsLocationwhich would make the URL look like this:

我刚刚注意到你正在调用你的控制器StatsLocationController——除非你的 C# 类被调用StatsLocationControllerController——这是错误的。Asp.Net MVC 假设对于一个控制器,会有一个被调用的类StatsLocationController,所以你应该引用你的控制器而没有这个Controller部分。所以在这个例子中你的控制器应该被调用StatsLocation,这将使 URL 看起来像这样:

@Url.Action("Visualization", "StatsLocation", new{ appId=d.AppId, userId=d.UserId})