Click here to Skip to main content
15,939,960 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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)

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" });

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.
Updated 9-Aug-17 22:43pm
Kornfeld Eliyahu Peter 9-Aug-17 14:24pm    
What is the actual HTML generated?
Member 12658724 9-Aug-17 14:31pm    

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

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"
location.href = "@Url.Action("Index", "Survey", new { id = UrlParameter.Optional })" + "/" + "@Model.CampaignGuid" + "/" + selectedValue
Share this answer
Member 12658724 10-Aug-17 8:25am    
I use your first code. It is working first time. The second time if I select "Spanish" again, the url becomes http://localhost:17671/Survey/ab7f6bda-9f5e-44d7-9bd2-63c24e6b6adb/Spanish/Spanish. You see there are two "Spanish" at the end of the url.

One thing I still don't understand is why you did not add id into the url? I thought you override the url, and the url didn't have the id value. Thus the id value would be lost?
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




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);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900