简体   繁体   English

如何在@ Html.ActionLink中的查询字符串参数中传递字符串

[英]How to pass String in Querystring Parameter in @Html.ActionLink

I have written following code: 我写了以下代码:

@Html.ActionLink("Edit", "EditPost", new { id = item.PostID })

It displays URL like .../Post/EditPost/32 but actually I want to display it like ...../Post/EditPost?ID=32 它显示的URL类似于.../Post/EditPost/32但实际上我想显示为...../Post/EditPost?ID=32

How is it possible in Razor View??? 在Razor View中怎么可能???

The URL construct is generally dictated by the route, if you want to force it to use the query string then just remove any notion of a parameter from it eg URL构造通常由路由决定,如果要强制其使用查询字符串,则只需从其中删除参数的任何概念即可,例如

routes.MapRoute(
    "EditPostRoute",
    "Post/EditPost",
    new { controller = "Edit", action = "EditPost" }
);

Your @Html.ActionLink code should generate .../Post/EditPost?ID=32 . 您的@Html.ActionLink代码应生成.../Post/EditPost?ID=32

Change Action Edit parameter from int to string . 将Action Edit参数从int更改为string Then it will display as you want 然后它将根据需要显示

public ActionResult Edit(string ID)
{
     =======
     =======
}

@Html.ActionLink("Edit", "EditPost", new { ID = item.PostID },null)

Another solution remove id = UrlParameter.Optional from the Global.asax/Route Table 另一种解决方案从Global.asax/Route Table删除id = UrlParameter.Optional

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM