2

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?

2 Answers 2

5

You can set the route value to an empty string

@Html.ActionLink("Link text", "Index", new { id = "" })
Sign up to request clarification or add additional context in comments.

1 Comment

Wonderful, I never though int could accept something like this. Its working, Thank you.
-2

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

1 Comment

What does this have to do with the question? I suggest you read it again carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.