Back or Transition to a specific view button (page) -APS.NET MVC3

I am using ASP.NET MVC3 for my web application. To display the button in the user interface, I use 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 put a button so that users can return to the previous page. To achieve this, I want to put a button in .cshtml, which takes a View Name and can

  • transition to a certain kind. (or)
  • To the previous view.

Any one of the above requirements will be useful for my application.

How can i do this? Any help or ideas would be helpful to me.

thanks

+6
source share
4 answers

If you want to send the user to your previous page, you can always use Request.UrlReferrer in your controller, but be careful that they can be easily fooled. Another way would be to save the previous location in the session for the user, then you could get it and connect to the button in your view.

Regarding the desired button, look here: Html.ActionLink as a button or image, not a link

Remember if you really need to add a back button? In most browsers, they are built-in; -)

+6
source

Here's what I implemented some time ago, not like your script, but it 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 needs to go:

 <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 displayed, the window.location part will look like this:

 window.location = '/GrantApplication'; 

Never mix your HTML and JavaScript code, but rather subscribe to the event after loading the control in the DOM.

+3
source

you can add a link like this.

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

here index is the name of the action that will be sent to the ceartain page. The answer is useful to me for you, if not, then I will send you the diff method.

+1
source

If you need styling (for example, adding an image) to a link, you can try the following:

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

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

+1
source

Source: https://habr.com/ru/post/900590/


All Articles