Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a question related to the Html.Action in MVC 4 I want to pass some Querystring Variables with it to the Details view

The code I have now is

System.Text.StringBuilder MobileData = new System.Text.StringBuilder();

MobileData.AppendFormat("<a style=\"text-align:left;\" data-role=\"button\"     onclick=\"window.location='" + @Url.Action("Taken_Detail", new { id = tk.ID }) + "';\" data-  ajax=\"true\" data-icon=\"alert\"><span class=\"AgenItems\">{1:dd-MM-yyyy}</span>", tk.ID, tk.Datum);

The problem is he would redirect me to localhost/PROJECTNAME/Home/Taken_Detail/2 what I want is Home/Taken_Detail?id=2 what am I missing here I am just starting to learn MVC 4, Every tip is welcome.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

This is because your routes contain the id parameter. Remove it from the routes and Url.Action will change the URL and add your parameter to the query string.

Example:

routes.MapRoute("Default", "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });

The id parameter will be put after the last slash if you specify it with Url.Action.

If you remove it:

routes.MapRoute("Default", "{controller}/{action}",
    new { controller = "Home", action = "Index" });

The resulting URL will have a query string containing the id.

share|improve this answer
    
Ty fero Super nice indeed :P hahah –  Stefan van de Laarschot May 21 '13 at 9:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.