0

How can I add parametr to link in MVC? When I use this (in controller) below code

Model model = GetModel();
string lang = "pl";
string viewName = "MyPage";
return View(viewName + "?lang=" + lang, model);

I get error The view 'MyPage?lang=pl' or its master was not found or no view engine supports the searched locations. The following locations were searched: ...

2
  • Whats do you intend to do with this query string parameter? Query string parameters in MVC get parsed against the relevant controllers, if you looking at passing an additional parameter for use in the page you should look into using the ViewBag Commented Dec 5, 2012 at 10:34
  • I want to have oportunity to send this url to other people Commented Dec 5, 2012 at 10:43

2 Answers 2

2

When you retun View result ASP.NET MVC simply renders this view for you. It has nothing to url. Url is entered by the user into browser to call your MVC action. If you whant to change the url you should user RedirectToAction result. E.g:

return RedirectToAction("MyPage", new {lang = "pl"});
Sign up to request clarification or add additional context in comments.

Comments

0

Use ViewData or an advanced ViweModel class to pass data to the view.

Model model = GetModel();
ViewData["lang"] = "pl";
return View("MyPage", model);

Of course if you referring to the query string in your view (which is bad) fix it to reference to the ViewData.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.