erw13n
erw13n

Reputation: 529

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?

Upvotes: 2

Views: 3278

Answers (2)

Shan Khan
Shan Khan

Reputation: 10319

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

Upvotes: -2

user3559349
user3559349

Reputation:

You can set the route value to an empty string

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

Upvotes: 5

Related Questions