Stefan van de Laarschot
Stefan van de Laarschot

Reputation: 2163

@Html.Action MVC 4 with QueryString Parameters

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.

Upvotes: 3

Views: 2535

Answers (1)

fero
fero

Reputation: 6183

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.

Upvotes: 5

Related Questions