簡體   English   中英

Asp.Net MVC-重定向到同一頁面而不傳遞參數

[英]Asp.Net MVC - redirect to same page without passing parameter

我正在嘗試在同一索引頁面中重定向以執行多項操作。

問題是在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
   }
}

有沒有辦法在View上不帶參數的情況下重定向到同一頁面?

您可以將路由值設置為空字符串

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

可以有多種方式。 生成HTML鏈接的Action Link

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM