【问题标题】:Asp.Net MVC - redirect to same page without passing parameterAsp.Net MVC - 重定向到同一页面而不传递参数
【发布时间】:2014-11-29 07:10:02
【问题描述】:

我正在尝试在执行多个操作的同一索引页面中重定向。

问题是在索引页面中使用 id 参数“http://example.com/Student/Index/1” Url.Action("Index") 或 Html.ActionLink("Index") 将始终生成:“http://example.com/Student/Index/1”而不是“http://example.com/Student

这发生在菜单和面包屑上。

控制器:

public ActionResult Index(int? id)
{
   if (id == null) {
      // Show List
   } else if (id <= 0) {
      // Show Create Form
   } else {
      //Show Edit Form
   }
}

有什么方法可以在视图上不带参数重定向到同一页面?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    您可以将路由值设置为空字符串

    @Html.ActionLink("Link text", "Index", new { id = "" })
    

    【讨论】:

    • 太棒了,我从来没有想过 int 可以接受这样的事情。它的工作,谢谢。
    【解决方案2】:

    可以有多种方式。 生成 html 链接的操作链接

    @Html.ActionLink("link text", "someaction", "somecontroller", new { id = "123" }, null)
    

    生成为:

    <a href="/somecontroller/someaction/123">link text</a>
    

    URL.action 只生成你需要放入 html 链接标签的 url

    Url.Action("someaction", "somecontroller", new { id = "123" })
    

    生成

    /somecontroller/someaction/123
    

    【讨论】:

    • 这与问题有什么关系?我建议你再仔细阅读一遍。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2021-08-18
    • 2013-03-11
    相关资源
    最近更新 更多