Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back button or Navigate To Specific View(Page) -ASP.NET MVC3

I am using ASP.NET MVC3 for my web application. To display a button in the User Interface I am using the following html in my View1.cshtml.

   <div class="demo">
                    <input type="submit" value="Save" title="Click here to create a comment" />
                    <input type="button" value="Cancel" onclick="location.href='/'"  title="Click here to cancel and go back to main menu" />
 </div>

I want to place a button so that users can go back to the previous page.. So to achieve this I want to place a button in the .cshtml which takes View Name and can

  • transfer to specific view. (or)
  • To the previous view.

Any one above requirements will be good for my application.

How can I do this? Any help or ideas will be useful to me.

Thank you

like image 725
Hari Gillala Avatar asked Nov 02 '11 09:11

Hari Gillala


4 Answers

If you want to send the user to their previous page, you can always use Request.UrlReferrer in your controller, but beware that it's possible for them to spoof this quite easily. Another way would be to store the previous location in the session for the user, then you could retrieve it and hook it up to the button in your view.

As for wanting a button, take a look here: Html.ActionLink as a button or an image, not a link

Mind you, is it really necessary to add a back button? Most browsers have them built in ;-)

like image 75
Tom Chantler Avatar answered Oct 16 '22 18:10

Tom Chantler


Here is what I implemented a while back, not similar to your scenario but can certainly help you forward:

My Html markup:

<button id="btnCancel" type="button" class="t-button">Cancel</button>

With jQuery I can specify where it need to go to:

<script type="text/javascript">

     $(document).ready(function () {
          $('#btnCancel').click(function () {
               window.location = '@Url.RouteUrl(Url.GrantApplicationIndex())';
          });
     });

</script>

My GrantApplicationIndex helper method code:

public static object GrantApplicationIndex(this UrlHelper instance)
{
     Check.Argument.IsNotNull(instance, "instance");

     return new { controller = "GrantApplication", action = "Index" };
}

When the page is rendered the window.location part will look like this:

window.location = '/GrantApplication';

Never mix your HTML and JavaScript code, rather subscribe to the event after the control is loaded to the DOM.

like image 42
Brendan Vogt Avatar answered Oct 16 '22 18:10

Brendan Vogt


you can add link like this.

  @Html.ActionLink("Back to List", "Index")

here index is action name which will send to ceartain page Reply me is it useful to you if not then i will send you diff method.

like image 1
user1006544 Avatar answered Oct 16 '22 17:10

user1006544


If you need some styling (like adding an image) to the link, you can try this:

 <a id="button" href="@Url.Action("Index", "Food")"></a>

U can put image in between the link tag. Hope this is what you mean :)

like image 1
shennyL Avatar answered Oct 16 '22 17:10

shennyL