【问题标题】:Iam trying to use Html.ActionLink to generate url with queryString parameter at the end of it我正在尝试使用 Html.ActionLink 在其末尾生成带有 queryString 参数的 url
【发布时间】:2022-01-26 03:35:13
【问题描述】:

我正在尝试使用Html.ActionLink 生成带有查询字符串参数的 URL,例如:

Controller/Action?id=5

但我得到的是:

Controller/Action/5

使用:

@Html.ActionLink((string)ViewBag.Back, "Action", "Controller", new{id = Model.Id }, new { })

如何将 id 参数作为 "?id=" 而不是在 "/" 之后传递 谢谢

【问题讨论】:

  • 该控制器操作的方法签名是什么,它的路由定义是什么?听起来该路线已经包含id 作为参数。这在某种程度上不起作用吗?
  • endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
  • public ActionResult Details(Nullable id)
  • 这就解释了。 id 已经是路由的一部分,并且 action 方法有一个 id 参数。这不适合你吗?为什么要将其更改为查询字符串参数?为此,您需要更改路由或更改操作方法参数名称。
  • 我不知道为什么它不能那样工作.. 感谢人的解释,我试着让它在两个方面都工作

标签: c# html asp.net-mvc razor frontend


【解决方案1】:

更改动作标题

public ActionResult Details(int? detailsId)

并调整链接

Html.ActionLink((string)ViewBag.Back, "Action", "Controller", new{detailsId = Model.Id }, new { })

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多