3

我有一个与 MVC 4 中的 Html.Action 相关的问题,我想将一些 Querystring 变量与它一起传递到详细信息视图

我现在的代码是

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

问题是他会将我重定向到 localhost/PROJECTNAME/Home/Taken_Detail/2 我想要的是 Home/Taken_Detail?id=2 我在这里缺少什么我刚刚开始学习 MVC 4,欢迎每个提示。

4

1 回答 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.

于 2013-05-21T09:35:28.197 回答