private static string GenerateLink( AjaxHelper ajaxHelper, string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary <string, object> htmlAttributes ) { TagBuilder tag = new TagBuilder("a") { InnerHtml = HttpUtility.HtmlEncode(linkText) }; tag.MergeAttributes(htmlAttributes); tag.MergeAttribute("href", targetUrl); if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { tag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } else { tag.MergeAttribute("onclick", GenerateAjaxScript(ajaxOptions, LinkOnClickFormat)); } return(tag.ToString(TagRenderMode.Normal)); }
public static ComponentBuilder <TConfig, FluentBootstrap.Forms.Form> AjaxForm <TConfig, TComponent>( this BootstrapHelper <TConfig, TComponent> helper, string formAction, System.Web.Mvc.Ajax.AjaxOptions ajaxOptions = null ) where TConfig : BootstrapConfig where TComponent : Component, ICanCreate <FluentBootstrap.Forms.Form> { var id = SomeRandom.Id(); ajaxOptions = ajaxOptions ?? new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "main-modal-body", OnSuccess = "indexViewModel.modalLoaded", OnFailure = "indexViewModel.postFailed", OnComplete = "Ladda.bind('.ladda-button')", }; var form = helper.Form() .AddAttribute("action", formAction) .AddAttribute("method", "post") .AddAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()) .SetId(id); return(form); }
public static string GetAjaxAttributes(AjaxOptions options) { if (null == options) return string.Empty; var dictionary = options.ToUnobtrusiveHtmlAttributes(); return string.Join(" ", dictionary .Select(c => string.Format("{0}={1}", c.Key, c.Value)) .ToArray()); }
public static MvcAnchor BeginActionLink(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, RouteValueDictionary htmlAttributes) { var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true); var builder = new TagBuilder("a"); builder.MergeAttributes(htmlAttributes); builder.MergeAttribute("href", targetUrl); builder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); ajaxHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); return new MvcAnchor(ajaxHelper.ViewContext); }
private static TagBuilder GenerateLink(int page, Func<int,string> generateUrl, bool active, string linkText, AjaxOptions ajaxOptions) { var li = new TagBuilder("li"); if (active) li.MergeAttribute("class", "active"); var a = new TagBuilder("a"); a.MergeAttribute("href", active ? "javascript:void()" : generateUrl(page)); a.SetInnerText(linkText); if(ajaxOptions != null) a.MergeAttributes<string, object>(ajaxOptions.ToUnobtrusiveHtmlAttributes()); li.InnerHtml = a.ToString(); return li; }
private static string GenerateLink( this AjaxHelper ajaxHelper, string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes ) { var a = new TagBuilder("a") { InnerHtml = linkText }; a.MergeAttributes<string, object>(htmlAttributes); a.MergeAttribute("href", targetUrl); a.MergeAttributes<string, object>(ajaxOptions.ToUnobtrusiveHtmlAttributes()); return a.ToString(TagRenderMode.Normal); }
public static ComponentBuilder <TConfig, FluentBootstrap.Buttons.LinkButton> AjaxLinkButton <TConfig, TComponent>( this BootstrapHelper <TConfig, TComponent> helper, string linkText, string href, string btnStyle = "btn-primary", System.Web.Mvc.Ajax.AjaxOptions ajaxOptions = null ) where TConfig : BootstrapConfig where TComponent : Component, ICanCreate <FluentBootstrap.Buttons.LinkButton> { ajaxOptions = ajaxOptions ?? new AjaxOptions() { OnSuccess = "indexViewModel.modalLoaded" }; var link = helper.LinkButton(linkText, href).AddCss("btn", btnStyle) .AddAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); return(link); }
private static MvcForm FormHelper( this AjaxHelper ajaxHelper, string formAction, AjaxOptions ajaxOptions, IDictionary <string, object> htmlAttributes ) { TagBuilder builder = new TagBuilder("form"); builder.MergeAttributes(htmlAttributes); builder.MergeAttribute("action", formAction); builder.MergeAttribute("method", "post"); ajaxOptions = GetAjaxOptions(ajaxOptions); if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { builder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } else { builder.MergeAttribute("onclick", FormOnClickValue); builder.MergeAttribute( "onsubmit", GenerateAjaxScript(ajaxOptions, FormOnSubmitFormat) ); } if (ajaxHelper.ViewContext.ClientValidationEnabled) { // forms must have an ID for client validation builder.GenerateId(ajaxHelper.ViewContext.FormIdGenerator()); } ajaxHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); MvcForm theForm = new MvcForm(ajaxHelper.ViewContext); if (ajaxHelper.ViewContext.ClientValidationEnabled) { ajaxHelper.ViewContext.FormContext.FormId = builder.Attributes["id"]; } return(theForm); }
private string GenerateLink(string targetUrl, IDictionary<string, object> htmlAttributes, AjaxOptions ajaxOptions) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttributes(htmlAttributes); tag.MergeAttribute("href", targetUrl); if (ajaxOptions != null) { if (_viewContext.UnobtrusiveJavaScriptEnabled) { tag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } else { tag.MergeAttribute("onclick", GenerateAjaxScript(ajaxOptions, LinkOnClickFormat)); } //tag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } return tag.ToString(TagRenderMode.StartTag); }
/// <summary> /// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID. /// </summary> /// <param name="options">The preferred Html.PagedList(...) style options.</param> /// <param name="ajaxOptions">The ajax options that will put into the link</param> /// <returns>The PagedListRenderOptions value passed in, with unobtrusive AJAX attributes added to the page links.</returns> public static PagedListRenderOptions EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions options, AjaxOptions ajaxOptions) { options.FunctionToTransformEachPageLink = (liTagBuilder, aTagBuilder) => { var liClass = liTagBuilder.Attributes.ContainsKey("class") ? liTagBuilder.Attributes["class"] ?? "" : ""; if (ajaxOptions != null && !liClass.Contains("disabled") && !liClass.Contains("active")) { foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) aTagBuilder.Attributes.Add(ajaxOption.Key, ajaxOption.Value.ToString()); } liTagBuilder.InnerHtml = aTagBuilder.ToString(); return liTagBuilder; }; return options; }
private static string GenerateLink(AjaxHelper ajaxHelper, string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) { TagBuilder tag = new TagBuilder("a") { InnerHtml = HttpUtility.HtmlEncode(linkText) }; tag.MergeAttributes(htmlAttributes); tag.MergeAttribute("href", targetUrl); if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { tag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } else { tag.MergeAttribute("onclick", GenerateAjaxScript(ajaxOptions, LinkOnClickFormat)); } return tag.ToString(TagRenderMode.Normal); }
private static MvcForm FormHelper(this AjaxHelper ajaxHelper, string formAction, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) { TagBuilder builder = new TagBuilder("form"); builder.MergeAttributes(htmlAttributes); builder.MergeAttribute("action", formAction); builder.MergeAttribute("method", "post"); ajaxOptions = GetAjaxOptions(ajaxOptions); if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { builder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } else { builder.MergeAttribute("onclick", FormOnClickValue); builder.MergeAttribute("onsubmit", GenerateAjaxScript(ajaxOptions, FormOnSubmitFormat)); } if (ajaxHelper.ViewContext.ClientValidationEnabled) { // forms must have an ID for client validation builder.GenerateId(ajaxHelper.ViewContext.FormIdGenerator()); } ajaxHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); MvcForm theForm = new MvcForm(ajaxHelper.ViewContext); if (ajaxHelper.ViewContext.ClientValidationEnabled) { ajaxHelper.ViewContext.FormContext.FormId = builder.Attributes["id"]; } return theForm; }
private static void GenerateLinkInternal( this AjaxHelper ajaxHelper, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes, bool includeImplicitMvcValues ) { string url = UrlHelper.GenerateUrl( routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, includeImplicitMvcValues ); TagBuilder linkTagBuilder = new TagBuilder( "a" ); linkTagBuilder.MergeAttributes( htmlAttributes ); linkTagBuilder.MergeAttribute( HtmlAttributes.Href, url ); if( !ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled ) { string onClickScript = string.Format( CultureInfo.InvariantCulture, LINK_ON_CLICK_FORMAT, BeginLinkExtensions.InvokeAjaxOptionsToJavascriptString( ajaxOptions ) ); linkTagBuilder.MergeAttribute( HtmlAttributes.Events.OnClick, onClickScript ); } else { linkTagBuilder.MergeAttributes<string, object>( ajaxOptions.ToUnobtrusiveHtmlAttributes() ); } ajaxHelper.ViewContext.Writer.Write( linkTagBuilder.ToString( TagRenderMode.StartTag ) ); }
private static string GenerateLinkInternal(RequestContext requestContext, RouteCollection routeCollection, string icon, ButtonMode status, string linkText, string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, string iconvariantclass, AjaxHelper ajaxHelper, AjaxOptions ajaxOptions) { //Make the link string targetUrl = UrlHelper.GenerateUrl(routeName, actionName, controllerName, null, null, null, routeValues, routeCollection, requestContext, false); //Tesing parameters if ((status & ButtonMode.IconOnly) != 0 && string.IsNullOrEmpty(icon)) throw new Exception("Only icon button without icon, how?"); //Prepare button caption. string caption = HttpUtility.HtmlEncode(linkText); bool disabled = (status & ButtonMode.Disabled) != 0; //Insert caption into a <span> with own CSS class TagBuilder captionTag = null; if ((status & ButtonMode.IconOnly) == 0) { captionTag = new TagBuilder("span") { InnerHtml = caption }; captionTag.AddCssClass("t4button-text"); } //Also insert icon into a <span>. Link this main CSS class which made by the T4 sprite-generator. //include unique icon name from helper method parameter TagBuilder iconTag = null; if (!string.IsNullOrEmpty(icon)) { iconTag = new TagBuilder("span"); iconTag.AddCssClass("t4icon " + IconDefaultClass + " " + icon); if (disabled) iconTag.AddCssClass(IconDisabledClass); if (!string.IsNullOrEmpty(iconvariantclass)) iconTag.AddCssClass(iconvariantclass); } //Making a wrapper around the caption and icon //Disabled button wil wrap a <div>. And <a> tag for normal button status TagBuilder buttonTag = new TagBuilder(disabled ? "div" : "a"); //rendering icon and caption <span> into wrapper tag if (iconTag == null) buttonTag.InnerHtml = captionTag.ToString(TagRenderMode.Normal); else if (captionTag == null) buttonTag.InnerHtml = iconTag.ToString(TagRenderMode.Normal); else buttonTag.InnerHtml = iconTag.ToString(TagRenderMode.Normal) + captionTag.ToString(TagRenderMode.Normal); //another attributes can arrive from helper method parameter buttonTag.MergeAttributes(htmlAttributes); buttonTag.Attributes.Add("role", "button"); if (disabled) { buttonTag.Attributes.Add("aria-disabled", "true"); //In 'disabled' status adding the default 'disbled' CSS class (we provided in .CssClassName= "disabled" in variant definition) buttonTag.AddCssClass(IconDisabledClass); } else { //URL for <a> tag buttonTag.MergeAttribute("href", targetUrl); buttonTag.Attributes.Add("aria-disabled", "false"); //If this is a Ajax helper call, will include all unobtrusive attributes. //This is all differences between Html.ActionLink and Ajax.ActionLink. if (ajaxHelper != null && ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) buttonTag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } //Parametrizing the title attribute if ((status & ButtonMode.IconOnly) != 0) { buttonTag.AddCssClass("t4button-icon-only"); buttonTag.Attributes.Add("title", caption); } else { buttonTag.AddCssClass(iconTag == null ? "t4button-text-only" : "t4button-text-icon"); if ((status & ButtonMode.ShowTitle) != 0) buttonTag.Attributes.Add("title", caption); } buttonTag.AddCssClass("t4button"); //Rendering all return buttonTag.ToString(TagRenderMode.Normal); }