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
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
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 ;-)
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.
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With