简体   繁体   English

Asp.Net MVC-重定向到同一页面而不传递参数

[英]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 " 问题是在ID参数为“ http://example.com/Student/Index/1 ”的索引页中,何时Url.Action(“ Index”)或Html.ActionLink(“ Index”)始终生成:“ http:/ /example.com/Student/Index/1 “代替” 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? 有没有办法在View上不带参数的情况下重定向到同一页面?

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

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

There can be multiple ways. 可以有多种方式。 Action Link which generates the html link 生成HTML链接的Action 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仅生成您需要放入html链接标记中的url

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

generates 生成

/somecontroller/someaction/123

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

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