3

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ty fero Super nice indeed :P hahah

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.