Pagine

venerdì 8 agosto 2014

Alertify ASP.NET MVC method helper

 

Per chi non conoscesse alertify è una libreria javascript per visualizzare sia finestre che messaggi di allerta. La potete trovare qui.

Visto però che programmo in ASP.NET MVC e visto che molti link sono dei link ajax, ho deciso di crearmi degli helper.

Questo il codice:

public static MvcHtmlString AlertifyConfirmActionLink(this HtmlHelper htmlHelper, string linkText,
string bottoneOk, string bottoneAnnulla,
string messaggio,
string messaggioOperazioneAnnullata,
System.Web.Mvc.ActionResult result, Dictionary<string, object> htmlAttributes)
{
return AlertifyConfirmActionLink(htmlHelper, linkText, bottoneOk, bottoneAnnulla, messaggioOperazioneAnnullata, messaggioOperazioneAnnullata,
result.GetT4MVCResult().Action, result.GetT4MVCResult().Controller, result.GetT4MVCResult().RouteValueDictionary, htmlAttributes);
}

public static MvcHtmlString AlertifyConfirmActionLink(this HtmlHelper htmlHelper, string linkText,
string bottoneOk, string bottoneAnnulla,
string messaggio,
string messaggioOperazioneAnnullata,
System.Web.Mvc.ActionResult result,
object htmlAttributes)
{
return AlertifyConfirmActionLink(htmlHelper, linkText, bottoneOk, bottoneAnnulla, messaggio, messaggioOperazioneAnnullata,
result.GetT4MVCResult().Action, result.GetT4MVCResult().Controller, result.GetT4MVCResult().RouteValueDictionary,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}


public static MvcHtmlString AlertifyConfirmActionLink(this HtmlHelper htmlHelper, string linkText,
string bottoneOk, string bottoneAnnulla,
string messaggio,
string messaggioOperazioneAnnullata,
string actionName, string controllerName, System.Web.Routing.RouteValueDictionary routeValues,
IDictionary<string, object> htmlAttributes)
{
string url = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, htmlHelper.RouteCollection,
htmlHelper.ViewContext.RequestContext, false);
TagBuilder tagBuilder = new TagBuilder("a")
{
InnerHtml = (!String.IsNullOrEmpty(linkText)) ? HttpUtility.HtmlEncode(linkText) : String.Empty
};
tagBuilder.MergeAttributes(htmlAttributes);


StringBuilder sb = new StringBuilder();
sb.Append("alertify.set({ labels: { ok: \"" + bottoneOk + "\", cancel: \"" + bottoneAnnulla + "\" } });").Append("\r\n");
sb.Append("alertify.confirm(\"" + messaggio + "\", function (e) {").Append("\r\n");
sb.Append("if (e) {").Append("\r\n");
sb.Append(" window.location='"+url+"'").Append("\r\n");
sb.Append("} else {").Append("\r\n");
if (!string.IsNullOrEmpty(messaggioOperazioneAnnullata))
{
sb.Append(" alertify.error(\"" + messaggioOperazioneAnnullata + "\");").Append("\r\n");
}
sb.Append("}").Append("\r\n");
sb.Append("});").Append("\r\n");

tagBuilder.MergeAttribute("onclick", sb.ToString());


MvcHtmlString stres = MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));

return stres;
}

I primi due metodi servono nel caso in cui utilizziate T4MVC, mentre nell’ultimo c’è il cuore dell’helper stesso.


Ovviamente per funzionare dovrete aver incluso la libreria alertify già nel vostro progetto con gli opportuni collegamenti.


Qui un suo utilizzo:

@Html.AlertifyConfirmActionLink("Procdura codificata", "Conferma", "Annulla", "Vuoi procedere?,
null, MVC.Operativo.ConfermaProcedura(Model.ProcId), new
{
@class ="btn btn-xs btn-primary"

})