简体   繁体   中英

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

I'm trying to redirect in same Index page that do multiple actions.

The problem is when in Index page with id parameter " http://example.com/Student/Index/1 " Url.Action("Index") or Html.ActionLink("Index") will always generate : " http://example.com/Student/Index/1 " instead of " http://example.com/Student "

This happen on Menu and Breadcrumb.

Controller :

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

Is there any way to redirect to same page without parameter on View?

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

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

There can be multiple ways. Action Link which generates the html link

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

generate as :

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

URL.action which only genrates url which you will need to put in html link tag

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

generates

/somecontroller/someaction/123

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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