Window.location.href将错误的参数传递给控制器 [英] Window.location.href pass a wrong parameter to the controller

查看:207
本文介绍了Window.location.href将错误的参数传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我想将参数传递给控制器​​。

In my view, I want to pass parameters to the controller.

window.location.href = "@Url.Action("index", "Survey")?languageName="
                   + selectedValue + "&id=@Model.CampaignGuid";





我的控制器:



My controller:

public ActionResult Index(Guid id, string languageName)
       {



我确信查询字符串是正确的。在查询字符串中,我看到languageName是西班牙语。但是当我在Index方法中设置一个断点时,它总是变成英语。即使我完全关闭页面并重新加载它,这种情况也会发生,就好像它被缓存在某个地方一样。我知道这很简单,但我以前从未碰过这个。



我有路线


I am sure that the query string is correct. In the query string, I saw the languageName is "Spanish". But when I set a break point in the Index method, it becomes "English" always.This happens even if I I totally close out of the page and reload it, almost as if it's cached somewhere. I know this is something simple, but I've never run across this before.

I have the route

routes.MapRoute("SurveyWelcomeRoute", "Survey/{id}/{languageName}",
                            defaults: new { action = "Index", controller = "Survey", languageName="English" });





我尝试了什么:



我在 ASP.NET MVC RedirectToAction传递错误的参数值? - 堆栈溢出 [ ^ ]

但我仍然不知道原因。我花了7个多小时。没有任何线索。



What I have tried:

I found a similar one at ASP.NET MVC RedirectToAction Passing Wrong Parameter Value? - Stack Overflow[^]
But I still don't know the cause. I have spent more than 7 hours. No clue.

推荐答案

有两件事......

一个是你混合不同的URL创建方法...有使用 http://site-address/page.html?param1 = value1& param2 = value2 进行旧的HTTP网址编码。还有MVC ROUTING使用 http:// site-address / page / value1 / value2 的形式。

你不能混合它们并期望正确的行为......

第二个是常见的错误。我们都忽略了有关UrlHelper.Action的文档,它解释了该函数将根据路由表中的匹配映射创建一个URL ...

在您的情况下,'Index'被省略,因为它是默认操作,比在请求时添加的ID但不是可选的('英语'不是出于与'索引'相同的原因)......然后你的部分......

There are two things...
One is your mixing different approaches for URL creation... There is the old-and-good HTTP URL ENCODING with the http://site-address/page.html?param1=value1&param2=value2. And there is the MVC ROUTING which uses the form of http://site-address/page/value1/value2.
You can not mix them and expect the correct behavior...
The second one is a common mistake. We all overlook the documentation about UrlHelper.Action, that explains that the function will create an URL according to the matching mapping from the route table...
In your case 'Index' omitted as it is the default action, than id added as it is requested but not optional ('English' isn't there for the same reason as 'Index')... then comes your part...
http://localhost:17671 -- site address
Survey -- controller
-- action is missing, as it has a default value
ab7f6bda-9f5e-44d7-9bd2-63c24e6b6adb -- id value (from model)
-- languageName missing, as it has a default value
?languageName=Spanish&id=ab7f6bda-9f5e-44d7-9bd2-63c24e6b6adb -- the part you wrote



所以 - 你有两个选择,一个是使用你已经从模型中获得的id值或使用另一个(在你的情况下它似乎是第一个)... 。


So - you have two options, one to use the id value you already have from the model or use an another (in your case it seems to be the first)...

location href = "@Url.Action("Index", "Survey")" + "/" + selectedValue"



Or

location.href = "@Url.Action("Index", "Survey", new { id = UrlParameter.Optional })" + "/" + "@Model.CampaignGuid" + "/" + selectedValue


我认为您不需要自定义路线,不确定它是否只会让您感到困惑。但是你的路线必须是你的路线



/ Survey / id / language



所以



/ Survey / b4cc6a5a-9f2a-4e61-9815-9709b43a26a3 / Spanish



你可以看到网址你产生的就是那样。此外,如果您想创建一个具有自定义路径的路径,则会有一个不同的方法调用



I don't think you need the custom route, not sure if it's only confusing you. But with your route the url needs to be

/Survey/id/language

so

/Survey/b4cc6a5a-9f2a-4e61-9815-9709b43a26a3/Spanish

as you can see the url you are generating is nothing like that. Also if you want to create a path that has a custom route there is a different method call for that

window.location.href = "@(Url.RouteUrl("SurveyWelcomeRoute", new { id = Model.CampaignGuid, languageName = UrlParameter.Optional }))/" + encodeURI(selectedValue);


这篇关于Window.location.href将错误的参数传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆