Ora invece vedremo come fare più o meno la stessa cosa ma aggiungendo il supporto Ajax.
Sostanzialmente quello che bisogna fare è costruire l'html tenendo conto di tutte le "cose" relative ad ajax (attributi, parametri, ecc) che il normale helper Ajax.ActionLink aggiunge.
In questo esempio ho usato la versione che utilizza i glifi, ma è ovviamente possibile usare lo stesso approccio anche nel caso si vogliano usare immagini normali.
/// <summary>
/// Create an Ajax.ActionLink with an associated glyphicon
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="linkText"></param>
/// <param name="actionName"></param>
/// <param name="controllerName"></param>
/// <param name="glyphicon"></param>
/// <param name="routeValues"></param>
/// <param name="htmlAttributes"></param>
/// <returns></returns>
public static MvcHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, string glyphicon, AjaxOptions ajaxOptions, RouteValueDictionary routeValues = null, object htmlAttributes = null)
{
//Example of result:
//<a id="btnShow" href="/Customers/ShowArtworks?customerId=1" data-ajax-update="#pnlArtworks" data-ajax-success="jsSuccess"
//data-ajax-mode="replace" data-ajax-method="POST" data-ajax-failure="jsFailure" data-ajax-confirm="confirm" data-ajax-complete="jsComplete"
//data-ajax-begin="jsBegin" data-ajax="true">
// <i class="glyphicon glyphicon-pencil"></i>
// <span>Edit</span>
//</a>
var builderI = new TagBuilder("i");
builderI.MergeAttribute("class", "glyphicon " + glyphicon);
string iTag = builderI.ToString(TagRenderMode.Normal);
string spanTag = "";
if (!string.IsNullOrEmpty(linkText))
{
var builderSPAN = new TagBuilder("span");
builderSPAN.InnerHtml = " " + linkText;
spanTag = builderSPAN.ToString(TagRenderMode.Normal);
}
//Create the "a" tag that wraps
var builderA = new TagBuilder("a");
var requestContext = HttpContext.Current.Request.RequestContext;
var uh = new UrlHelper(requestContext);
builderA.MergeAttribute("href", uh.Action(actionName, controllerName, routeValues));
//Ajax section
builderA.MergeAttribute("data-ajax", "true");
builderA.MergeAttribute("data-ajax-update", ajaxOptions.UpdateTargetId.StartsWith("#") ? ajaxOptions.UpdateTargetId : "#" + ajaxOptions.UpdateTargetId);
if (!string.IsNullOrEmpty(ajaxOptions.InsertionMode.ToString()))
builderA.MergeAttribute("data-ajax-mode", ajaxOptions.InsertionMode.ToString());
if (!string.IsNullOrEmpty(ajaxOptions.OnBegin))
builderA.MergeAttribute("data-ajax-begin", ajaxOptions.OnBegin);
if (!string.IsNullOrEmpty(ajaxOptions.OnComplete))
builderA.MergeAttribute("data-ajax-complete", ajaxOptions.OnComplete);
if (!string.IsNullOrEmpty(ajaxOptions.OnFailure))
builderA.MergeAttribute("data-ajax-failure", ajaxOptions.OnFailure);
if (!string.IsNullOrEmpty(ajaxOptions.OnSuccess))
builderA.MergeAttribute("data-ajax-success", ajaxOptions.OnSuccess);
if (!string.IsNullOrEmpty(ajaxOptions.Confirm))
builderA.MergeAttribute("data-ajax-confirm", ajaxOptions.Confirm);
if (!string.IsNullOrEmpty(ajaxOptions.HttpMethod))
builderA.MergeAttribute("data-ajax-method", ajaxOptions.HttpMethod);
if (htmlAttributes != null)
{
IDictionary<string, object> attributes = new RouteValueDictionary(htmlAttributes);
builderA.MergeAttributes(attributes);
}
builderA.InnerHtml = iTag + spanTag;
return new MvcHtmlString(builderA.ToString(TagRenderMode.Normal));
}
Come si può vedere, il codice è simile a quello che abbiamo utilizzato nei post precedenti con l'eccezione del fatto che stiamo estendendo un "AjaxHelper" invece che un "HtmlHelper" come accadeva prima e che abbiamo aggiunto una "ajax section".
Nessun commento:
Posta un commento