[Dev tip] ASP.NET MVC back button with html helper method



public static MvcHtmlString BackButton(this HtmlHelper helper, string buttonName = null, string @class = "btn btn-default")
{
if (string.IsNullOrEmpty(buttonName))
{
buttonName = Hydra.Common.I18n.ResourceConstants.Button_Back;
}

var previousURI = (System.Uri)helper.ViewContext.RequestContext.HttpContext.Request.UrlReferrer;
string result = "<a href=\"{0}\" class=\"{1}\">{2}</a>".FormatWith(previousURI, @class, buttonName);

return new MvcHtmlString(result);
}

 

on view

@Html.BackButton()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s