public void ToUnobtrusiveHtmlAttributesWithAllowCache() { // Arrange AjaxOptions options = new AjaxOptions { UpdateTargetId = "someId", HttpMethod = "GET", AllowCache = true, Url = "http://someurl.com", OnComplete = "some_complete_function" }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(7, attributes.Count); Assert.Equal("true", attributes["data-ajax"]); Assert.Equal("GET", attributes["data-ajax-method"]); Assert.Equal("http://someurl.com", attributes["data-ajax-url"]); Assert.Equal("#someId", attributes["data-ajax-update"]); Assert.Equal("true", attributes["data-ajax-cache"]); Assert.Equal("replace", attributes["data-ajax-mode"]); Assert.Equal("some_complete_function", attributes["data-ajax-complete"]); }
private string GeneratePageLink(string linkText, int pageNumber) { var pageLinkValueDictionary = new RouteValueDictionary(linkWithoutPageValuesDictionary) { { "page", pageNumber } }; var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext, pageLinkValueDictionary); if (virtualPathForArea == null) { return(null); } var stringBuilder = new StringBuilder("<a"); if (ajaxOptions != null) { foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) { stringBuilder.AppendFormat(" {0}=\"{1}\"", ajaxOption.Key, ajaxOption.Value); } } stringBuilder.AppendFormat(" href=\"{0}\">{1}</a>", virtualPathForArea.VirtualPath, linkText); return(stringBuilder.ToString()); }
public MvcForm Form_Edit <TModel>(AjaxHelper <TModel> helper) { var htmlHelper = new HtmlHelper <TModel>(helper.ViewContext, helper.ViewDataContainer); var model = helper.ViewData.Model; var modelIdValue = ModelHelper <TModel> .GetPrimaryKeyValues(model)[0]; var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection); var url = urlHelper.Action("GetEntityJson"); var button = htmlHelper.Button() .SetText(ConstViews.BTN_Save) .IsSubmitBtn(true); AjaxOptions ajaxOptions = new AjaxOptions() { OnSuccess = "new function(){" + GenerateEditElementJSCode(modelIdValue, url) + "}" }; var ajaxOptionsToHtmlAttr = ajaxOptions.ToUnobtrusiveHtmlAttributes(); return(GenerateForm( helper, ConstantHelper.ActionNameEdit, ConstantHelper.AreaNameManageData, null, ajaxOptionsToHtmlAttr, Form_Edit_Body(helper) + button)); }
/// <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 PagedListRenderOptionsBase EnableUnobtrusiveAjaxReplacing(PagedListRenderOptionsBase options, AjaxOptions ajaxOptions) { if (options is PagedListRenderOptions) { ((PagedListRenderOptions)options).FunctionToTransformEachPageLink = (liTagBuilder, aTagBuilder) => { var liClass = liTagBuilder.Attributes.ContainsKey("class") ? liTagBuilder.Attributes["class"] ?? "" : ""; // && !liClass.Contains("active") Éú³Éactive html±êÇ© if (ajaxOptions != null && !liClass.Contains("disabled")) { foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) { aTagBuilder.Attributes.Add(ajaxOption.Key, ajaxOption.Value.ToString()); } } liTagBuilder.InnerHtml = aTagBuilder.ToString(); return(liTagBuilder); }; } return(options); }
public void ToUnobtrusiveHtmlAttributes() { // Arrange AjaxOptions options = new AjaxOptions { InsertionMode = InsertionMode.InsertBefore, Confirm = "confirm", HttpMethod = "POST", LoadingElementId = "loadingElement", LoadingElementDuration = 450, UpdateTargetId = "someId", Url = "http://someurl.com", OnBegin = "some_begin_function", OnComplete = "some_complete_function", OnFailure = "some_failure_function", OnSuccess = "some_success_function", }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.AreEqual(12, attributes.Count); Assert.AreEqual("true", attributes["data-ajax"]); Assert.AreEqual("confirm", attributes["data-ajax-confirm"]); Assert.AreEqual("POST", attributes["data-ajax-method"]); Assert.AreEqual("#loadingElement", attributes["data-ajax-loading"]); Assert.AreEqual(450, attributes["data-ajax-loading-duration"]); Assert.AreEqual("http://someurl.com", attributes["data-ajax-url"]); Assert.AreEqual("#someId", attributes["data-ajax-update"]); Assert.AreEqual("before", attributes["data-ajax-mode"]); Assert.AreEqual("some_begin_function", attributes["data-ajax-begin"]); Assert.AreEqual("some_complete_function", attributes["data-ajax-complete"]); Assert.AreEqual("some_failure_function", attributes["data-ajax-failure"]); Assert.AreEqual("some_success_function", attributes["data-ajax-success"]); }
public static IHtmlString BsLink(this HtmlHelper htmlHelper, object innerText, string buttonClass, string iconClass, string linkHref, object htmlAttributes = null, AjaxOptions ajaxOptions = null) { var mainAttrs = new Dictionary <string, object>(); if (!String.IsNullOrWhiteSpace(linkHref)) { mainAttrs.Add("href", linkHref); } if (!String.IsNullOrWhiteSpace(buttonClass)) { mainAttrs.Add("class", buttonClass); } var link = new FluentTagBuilder("a") .AddAttributes(mainAttrs) .AddAttributes(htmlAttributes) .AddContent(htmlHelper.Icon(iconClass, innerText)); if (ajaxOptions != null) { link.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } return(link); }
private string GeneratePageLink(string linkText, int pageNumber) { var routeDataValues = viewContext.RequestContext.RouteData.Values; RouteValueDictionary pageLinkValueDictionary; // Avoid canonical errors when page count is equal to 1. if (pageNumber == 1) { pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary); if (routeDataValues.ContainsKey("page")) { routeDataValues.Remove("page"); } } else { pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary) { { "page", pageNumber } }; } // To be sure we get the right route, ensure the controller and action are specified. if (!pageLinkValueDictionary.ContainsKey("controller") && routeDataValues.ContainsKey("controller")) { pageLinkValueDictionary.Add("controller", routeDataValues["controller"]); } if (!pageLinkValueDictionary.ContainsKey("action") && routeDataValues.ContainsKey("action")) { pageLinkValueDictionary.Add("action", routeDataValues["action"]); } // 'Render' virtual path. var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext, pageLinkValueDictionary); if (virtualPathForArea == null) { return(null); } var stringBuilder = new StringBuilder("<a"); if (ajaxOptions != null) { foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) { stringBuilder.AppendFormat(" {0}=\"{1}\"", ajaxOption.Key, ajaxOption.Value); } } stringBuilder.AppendFormat(" href=\"{0}\">{1}</a>", virtualPathForArea.VirtualPath, linkText); return(stringBuilder.ToString()); }
public static NameValueCollection ToQueryString(this AjaxOptions ajaxOptions) { NameValueCollection retVal = HttpUtility.ParseQueryString("?"); foreach (var item in ajaxOptions.ToUnobtrusiveHtmlAttributes()) { retVal.Add(item.Key, item.Value.ToString()); } return(retVal); }
public static MvcAnchor BeginActionLink(this AjaxHelper helper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, RouteValueDictionary htmlAttributes) { var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, helper.RouteCollection, helper.ViewContext.RequestContext, true); var builder = new TagBuilder("a"); builder.MergeAttributes(htmlAttributes); builder.MergeAttribute("href", targetUrl); builder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); helper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); return(new MvcAnchor(helper.ViewContext)); }
public void ToUnobtrusiveHtmlAttributesWithEmptyOptions() { // Arrange AjaxOptions options = new AjaxOptions(); // Act IDictionary <string, object> attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Single(attributes); Assert.Equal("true", attributes["data-ajax"]); }
public static MvcHtmlString PopoverConfirmLink(this AjaxHelper helper, string innerHtml, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes, ConfirmInfo confirmInfo, params string[] permissionCodes) { RouteValueDictionary parameters = HtmlHelper.AnonymousObjectToHtmlAttributes(routeValues); RouteValueDictionary attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); ajaxOptions.ToUnobtrusiveHtmlAttributes(); //confirmInfo.PopulateAjaxOptions(ajaxOptions); //confirmInfo.PopulateParameters(parameters); HtmlHelper htmlHelper = new HtmlHelper(helper.ViewContext, helper.ViewDataContainer, helper.RouteCollection); return(htmlHelper.Link(innerHtml, actionName, null, parameters, attributes, permissionCodes)); }
private static string GeneratePageSizeDropdown(ViewContext viewContext, int pageSize, RouteValueDictionary action, AjaxOptions ajaxOptions) { var pageLink = new RouteValueDictionary(action) { { "page", 1 } }; var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext, pageLink); if (virtualPathForArea == null) { return(null); } var stringBuilder = new StringBuilder("<div style='float:left'><form method=\"get\""); if (ajaxOptions != null) { foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) { stringBuilder.AppendFormat(" {0}=\"{1}\"", ajaxOption.Key, ajaxOption.Value); } } stringBuilder.AppendFormat(" action=\"{0}\" >", virtualPathForArea.VirtualPath); stringBuilder.Append(" Số kết quả/trang: <select name=\"pageSize\" style=\"width:60px;\" onchange=\"$(this).parents('form:first').submit()\">"); for (var i = 10; i <= 30; i += 5) { stringBuilder.Append("<option value=\"" + i + "\""); if (i == pageSize) { stringBuilder.Append(" selected=\"selected\""); } stringBuilder.Append(" >" + i + "</option>"); } stringBuilder.Append("</select></form></div>"); string pageTime = ""; if (_timePage != null) { pageTime = " - thời gian xử lý: " + _timePage + " "; } stringBuilder.Append("<div style='float:right; margin-top:5px'> " + _totalRecords + " kết quả trong " + _pageCount + " trang" + pageTime + " </div>"); return(stringBuilder.ToString()); }
public void ToUnobtrusiveHtmlAttributesEscapesClientSideIdentifiers(string id, string expected) { // Arrange AjaxOptions options = new AjaxOptions { UpdateTargetId = id, LoadingElementId = id }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(expected, attributes["data-ajax-update"]); Assert.Equal(expected, attributes["data-ajax-loading"]); }
/// <summary> /// Returns an anchor element that contains the an encrypted URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. /// </summary> /// <param name="helper">The AJAX helper.</param> /// <param name="innerHtml">The inner HTML of the anchor element.</param> /// <param name="actionName"> The name of the action method.</param> /// <param name="controllerName">The name of the controller.</param> /// <param name="routeValues">An object that contains the parameters for a route. The parameters will be encrypted into one (q) parameter.</param> /// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param> /// <param name="ajaxOptions">An object that provides options for the asynchronous request.</param> /// <param name="permissionCodes">permissions that have to be checked to show the element.</param> /// <returns>An encrypted HREF anchor element (a element) if current user is authenticated and has the specified permission codes.</returns> public static MvcHtmlString Link(this AjaxHelper helper, string innerHtml, string actionName, string controllerName, RouteValueDictionary routeValues, RouteValueDictionary htmlAttributes, AjaxOptions ajaxOptions, params string[] permissionCodes) { ajaxOptions = (ajaxOptions ?? new AjaxOptions()); var ajaxOptionAttributes = ajaxOptions.ToUnobtrusiveHtmlAttributes(); //add ajax attributes to htmlAttributes foreach (var item in ajaxOptionAttributes) { htmlAttributes[item.Key] = item.Value; } return(GenerateEncryptedLinkInternal(helper.ViewContext.RequestContext, helper.RouteCollection, innerHtml, null, actionName, controllerName, null, null, null, routeValues, htmlAttributes, permissionCodes)); }
public static MvcHtmlString PagerDropdownForThumbnail(this HtmlHelper htmlHelper, int pagesize, string actionName, string controllerName = "", AjaxOptions ajaxOptions = null, object routeValues = null, object htmlAttributes = null, int staticPageSize = 0) { var builder = new TagBuilder("select"); var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); string value = pagesize.ToString(); string url = actionName; if (routeValues != null) { var rValues = new System.Web.Routing.RouteValueDictionary(routeValues); if (controllerName == "") { url = urlHelper.Action(actionName, rValues); } else { url = urlHelper.Action(actionName, controllerName, rValues); } } else { if (controllerName != "") { url = urlHelper.Action(actionName, controllerName); } } if (htmlAttributes != null) { builder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } if (ajaxOptions != null) { builder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } StringBuilder options = new StringBuilder(); options = options.Append("<option value='10' " + ((!string.IsNullOrEmpty(value) && value == "10") ? "selected=selected" : "") + ">10</option>"); options = options.Append("<option value='20' " + ((!string.IsNullOrEmpty(value) && value == "20") ? "selected=selected" : "") + ">20</option>"); options = options.Append("<option value='30' " + ((!string.IsNullOrEmpty(value) && value == "30") ? "selected=selected" : "") + ">30</option>"); options = options.Append("<option value='50' " + ((!string.IsNullOrEmpty(value) && value == "50") ? "selected=selected" : "") + ">50</option>"); options = options.Append("<option value='100' " + ((!string.IsNullOrEmpty(value) && value == "100") ? "selected=selected" : "") + ">100</option>"); builder.InnerHtml = options.ToString(); //builder.Attributes["onchange"] = "pagerDropdownChange(this,'" + url + "')"; builder.Attributes["onchange"] = "pagerThumbViewDropdownChange(this,'" + url + "' , " + value + ")";//pass old page value on drop down change builder.Attributes["class"] = "pagerDropdown form-control input-sm input-xsmall input-inline"; return(MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal))); }
public static MvcHtmlString GenerateAjaxUnobtrusiveHtmlAttributes(AjaxOptions options) { var attribues = options.ToUnobtrusiveHtmlAttributes(); StringBuilder sb = new StringBuilder(); foreach (var item in attribues) { sb.Append($"{item.Key}=\"{item.Value}\" "); } var result = MvcHtmlString.Create(sb.ToString()); return(result); }
private static string GenerateLink(string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary <string, object> htmlAttributes) { var a = new TagBuilder("a") { InnerHtml = linkText }; a.MergeAttributes(htmlAttributes); a.MergeAttribute("href", targetUrl); a.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); return(a.ToString(TagRenderMode.Normal)); }
public void ToUnobtrusiveHtmlAttributesWithOnlyUpdateTargetId() { // Arrange AjaxOptions options = new AjaxOptions { UpdateTargetId = "someId" }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(3, attributes.Count); Assert.Equal("true", attributes["data-ajax"]); Assert.Equal("#someId", attributes["data-ajax-update"]); Assert.Equal("replace", attributes["data-ajax-mode"]); // Only added when UpdateTargetId is set }
public static MvcHtmlString RenderAction(this AjaxHelper helper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions) { var urlHelper = new UrlHelper(helper.ViewContext.RequestContext); var url = urlHelper.Action(actionName, controllerName, routeValues); var tagBuilder = new TagBuilder("div"); if (ajaxOptions != null) { tagBuilder.MergeAttributes <string, object>(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } tagBuilder.MergeAttribute("data-ajax-action-link", "true"); tagBuilder.MergeAttribute("data-href", url); return(new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal))); }
private static string GenerateLink( Func <dynamic, HelperResult> linkHtml, string targetUrl, AjaxOptions ajaxOptions, IDictionary <string, object> htmlAttributes ) { var a = new TagBuilder("a") { InnerHtml = linkHtml(null).ToString() }; a.MergeAttributes(htmlAttributes); a.MergeAttribute("href", targetUrl); a.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); return(a.ToString(TagRenderMode.Normal)); }
public void ToUnobtrusiveHtmlAttributesWithUpdateTargetIdAndExplicitInsertionMode() { // Arrange AjaxOptions options = new AjaxOptions { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "someId" }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.AreEqual(3, attributes.Count); Assert.AreEqual("true", attributes["data-ajax"]); Assert.AreEqual("#someId", attributes["data-ajax-update"]); Assert.AreEqual("after", attributes["data-ajax-mode"]); }
//public static MvcHtmlString PageSizeFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> pageSizeExpression, IDictionary<string, string> pageSizes, string formId, string styleClass) //{ // var pageSizeFieldId = htmlHelper.FieldIDFor(pageSizeExpression); // var selectedValue = (int)ModelMetadata.FromLambdaExpression(pageSizeExpression, htmlHelper.ViewData).Model; // var formSubmitScriptTemplate = "var value = $('#{1}').val();$('form#{0}').trigger('reset');$('#{1}').val(value);$('form#{0}').trigger('submit');"; // return htmlHelper.DropDownListFor(pageSizeExpression, new SelectList(pageSizes, "Key", "Value", selectedValue), new { @class = styleClass, onchange = String.Format(formSubmitScriptTemplate, formId, pageSizeFieldId) }); //} public static MvcHtmlString SortColumnFor1 <TModel, TSortName, TSortOrder>(this AjaxHelper <TModel> htmlHelper, BaseFilterViewModel model, Expression <Func <TModel, TSortName> > sortNameExpression,//m => m.Filter.SortColumn Expression <Func <TModel, TSortOrder> > sortOrderExpression, Func <BaseFilterViewModel, string> sortingUrl, AjaxOptions ajaxOptions, string sortColumnTitle, string ascendingStyleClass, string descendingStyleClass) { var currentSortName = (string)ModelMetadata.FromLambdaExpression(sortNameExpression, htmlHelper.ViewData).Model; var currentSortOrder = (bool)ModelMetadata.FromLambdaExpression(sortOrderExpression, htmlHelper.ViewData).Model; var isCurrentSorted = currentSortName == sortColumnTitle; var sortOrder = isCurrentSorted ? !currentSortOrder : true; model.SetPropertyValue(m => m.SortColumn, sortColumnTitle); model.SetPropertyValue(m => m.PageNumber, model.PageNumber); model.SetPropertyValue(m => m.IsAscending, sortOrder); model.SetPropertyValue(m => m.IsApply, model.IsApply); var builder = new TagBuilder("a"); builder.SetInnerText(sortColumnTitle); builder.Attributes.Add("href", sortingUrl(null)); model.SetPropertyValue(m => m.SortColumn, currentSortName); model.SetPropertyValue(m => m.IsAscending, currentSortOrder); if (ajaxOptions != null) { foreach (var ajaxOption in ajaxOptions.ToUnobtrusiveHtmlAttributes()) { builder.Attributes.Add(ajaxOption.Key, ajaxOption.Value.ToString()); } } if (isCurrentSorted) { builder.AddCssClass(currentSortOrder ? ascendingStyleClass : descendingStyleClass); } return(MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal))); }
public static ComponentBuilder <TConfig, FluentBootstrap.Dropdowns.DropdownLink> AjaxDropdownLinkButton <TConfig, TComponent>( this BootstrapHelper <TConfig, TComponent> helper, string linkText, string href, TextState state = TextState.Primary ) where TConfig : BootstrapConfig where TComponent : Component, ICanCreate <DropdownLink> { var ajaxOptions = new AjaxOptions() { OnSuccess = "indexViewModel.modalLoaded" }; var attributes = ajaxOptions.ToUnobtrusiveHtmlAttributes(); var link = helper.DropdownLink(linkText, href).AddAttributes(attributes); return(link); }
public void ToUnobtrusiveHtmlAttributesWithUpdateTargetIdAndExplicitInsertionMode(InsertionMode mode, string expectedMode) { // Arrange AjaxOptions options = new AjaxOptions { InsertionMode = mode, UpdateTargetId = "someId" }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(3, attributes.Count); Assert.Equal("true", attributes["data-ajax"]); Assert.Equal("#someId", attributes["data-ajax-update"]); Assert.Equal(expectedMode, attributes["data-ajax-mode"]); }
static CommandForm <T> FormHelper <T>(this AjaxHelper ajaxHelper, string formAction, string action, string controller, AjaxOptions ajaxOptions, IDictionary <string, object> htmlAttributes) where T : ICommand, new() { htmlAttributes = htmlAttributes ?? new Dictionary <string, object>(); RenderCommandFormEventsScriptIfNotOnPage(ajaxHelper); var builder = new TagBuilder("form"); builder.MergeAttribute("action", formAction); builder.MergeAttributes <string, object>(htmlAttributes); builder.MergeAttribute("method", "post"); builder.MergeAttribute("data-commandForm", "", true); if (ajaxHelper.ViewContext.ClientValidationEnabled && !htmlAttributes.ContainsKey("id")) { builder.GenerateId(typeof(T).Name); } var idKey = "id"; var formId = builder.Attributes.ContainsKey(idKey) ? builder.Attributes["id"] : Guid.NewGuid().ToString(); ajaxOptions = ajaxOptions ?? new AjaxOptions(); ajaxOptions.OnSuccess = string.IsNullOrEmpty(ajaxOptions.OnSuccess) ? "Bifrost.commands.commandFormEvents.onSuccess('#" + formId + "', data)" : ajaxOptions.OnSuccess; ajaxOptions.HttpMethod = string.IsNullOrEmpty(ajaxOptions.HttpMethod) ? "Get" : ajaxOptions.HttpMethod; if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { builder.MergeAttributes <string, object>(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } ajaxHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); var form = new CommandForm <T>(ajaxHelper.ViewContext); form.Action = action; form.Controller = controller; if (ajaxHelper.ViewContext.ClientValidationEnabled) { ajaxHelper.ViewContext.FormContext.FormId = formId; } return(form); }
public static MvcHtmlString DropDownList(this AjaxHelper html, string action, string controller, RouteValueDictionary routeValues, AjaxOptions options, IEnumerable <SelectListItem> selectItems, IDictionary <string, object> listHtmlAttributes) { var url = new UrlHelper(html.ViewContext.RequestContext); // Wrap it in a form var formBuilder = new TagBuilder("form"); // build the <select> tag var listBuilder = new TagBuilder("select"); if (listHtmlAttributes != null && listHtmlAttributes.Count > 0) { listBuilder.MergeAttributes(listHtmlAttributes); } StringBuilder optionHTML = new StringBuilder(); foreach (SelectListItem item in selectItems) { var optionBuilder = new TagBuilder("option"); optionBuilder.MergeAttribute("value", item.Value); optionBuilder.InnerHtml = item.Text; if (item.Selected) { optionBuilder.MergeAttribute("selected", "selected"); } //optionBuilder.Attributes["onchange"] = "($this.form).attr('action', '" + url.Action(action).Replace("___", item.Value) + "');$(this.form).submit();"; optionHTML.Append(optionBuilder.ToString()); } listBuilder.InnerHtml = optionHTML.ToString(); listBuilder.Attributes["onchange"] = "$(this.form).attr('action', '/" + controller + "/" + action + "?pageSize=' + $(this).first('option:selected').val());$(this).first('option:selected').val();$(this.form).submit();"; formBuilder.InnerHtml = listBuilder.ToString(); foreach (var ajaxOption in options.ToUnobtrusiveHtmlAttributes()) { formBuilder.MergeAttribute(ajaxOption.Key, ajaxOption.Value.ToString()); } string formHtml = formBuilder.ToString(TagRenderMode.Normal); return(MvcHtmlString.Create(formHtml)); }
public MvcForm Form_Search <TModel>(AjaxHelper <TModel> helper) { var ajaxOptions = new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = ConstantHelper.FullModalContentId, LoadingElementId = ConstantHelper.LoaderId, OnSuccess = "new function(){openModal('" + ConstantHelper.FullModalId + "',true)}" }; var ajaxOptionsToHtmlAttr = ajaxOptions.ToUnobtrusiveHtmlAttributes(); return(GenerateSearchForm( helper, ConstantHelper.ActionNameSearch, ConstantHelper.AreaNameSearch, null, ajaxOptionsToHtmlAttr, Form_Search_Body(helper))); }
protected override bool UpdateTag(TagBuilder tag) { var ajaxOptions = new AjaxOptions() { AllowCache = Context.AllowCache, Confirm = Context.Confirm, InsertionMode = Context.InsertionMode, LoadingElementDuration = Context.LoadingElementDuration, LoadingElementId = Context.LoadingElementId, OnBegin = Context.OnBegin, OnComplete = Context.OnComplete, OnFailure = Context.OnFailure, OnSuccess = Context.OnSuccess, UpdateTargetId = Context.UpdateTargetId, HttpMethod = Context.Method.ToString(), }; tag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); return(base.UpdateTag(tag)); }
private string GeneratePageLink(string linkText, int pageNumber) { var virtualPath = HttpContext.Current.Request.Path; virtualPath = virtualPath.EndsWith("/") ? virtualPath.Remove(virtualPath.LastIndexOf('/')) : virtualPath; var stringBuilder = new StringBuilder("<a"); if (_ajaxOptions != null) { foreach (var ajaxOption in _ajaxOptions.ToUnobtrusiveHtmlAttributes()) { stringBuilder.AppendFormat(" {0}=\"{1}\"", ajaxOption.Key, ajaxOption.Value); } } stringBuilder.AppendFormat(" href=\"{0}/?page={1}\">{2}</a>", virtualPath, pageNumber, linkText); return(stringBuilder.ToString()); }
private static string GenerateLink(AjaxHelper ajaxHelper, string innerHtml, string targetUrl, AjaxOptions ajaxOptions, IDictionary <string, object> htmlAttributes) { TagBuilder tag = new TagBuilder("a") { InnerHtml = (!string.IsNullOrEmpty(innerHtml)) ? innerHtml : string.Empty }; tag.MergeAttributes(htmlAttributes); tag.MergeAttribute("href", targetUrl); if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { tag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } else { throw new InvalidOperationException("Unobtrusive Java Script must be enabled."); } return(tag.ToString(TagRenderMode.Normal)); }
public void ToUnobtrusiveHtmlAttributesWithUpdateTargetIdAndExplicitInsertionMode() { // Arrange AjaxOptions options = new AjaxOptions { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "someId" }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(3, attributes.Count); Assert.Equal("true", attributes["data-ajax"]); Assert.Equal("#someId", attributes["data-ajax-update"]); Assert.Equal("after", attributes["data-ajax-mode"]); }
public void ToUnobtrusiveHtmlAttributesEscapesClientSideIdentifiers(string id, string expected) { // Arrange AjaxOptions options = new AjaxOptions { UpdateTargetId = id, LoadingElementId = id }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(expected, attributes["data-ajax-update"]); Assert.Equal(expected, attributes["data-ajax-loading"]); }
public void ToUnobtrusiveHtmlAttributesWithOnlyUpdateTargetId() { // Arrange AjaxOptions options = new AjaxOptions { UpdateTargetId = "someId" }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(3, attributes.Count); Assert.Equal("true", attributes["data-ajax"]); Assert.Equal("#someId", attributes["data-ajax-update"]); Assert.Equal("replace", attributes["data-ajax-mode"]); // Only added when UpdateTargetId is set }
public void ToUnobtrusiveHtmlAttributesWithAllowCache() { // Arrange AjaxOptions options = new AjaxOptions { UpdateTargetId = "someId", HttpMethod = "GET", AllowCache = true, Url = "http://someurl.com", OnComplete = "some_complete_function" }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(7, attributes.Count); Assert.Equal("true", attributes["data-ajax"]); Assert.Equal("GET", attributes["data-ajax-method"]); Assert.Equal("http://someurl.com", attributes["data-ajax-url"]); Assert.Equal("#someId", attributes["data-ajax-update"]); Assert.Equal("true", attributes["data-ajax-cache"]); Assert.Equal("replace", attributes["data-ajax-mode"]); Assert.Equal("some_complete_function", attributes["data-ajax-complete"]); }
public void ToUnobtrusiveHtmlAttributes() { // Arrange AjaxOptions options = new AjaxOptions { InsertionMode = InsertionMode.InsertBefore, Confirm = "confirm", HttpMethod = "POST", LoadingElementId = "loadingElement", LoadingElementDuration = 450, UpdateTargetId = "someId", Url = "http://someurl.com", OnBegin = "some_begin_function", OnComplete = "some_complete_function", OnFailure = "some_failure_function", OnSuccess = "some_success_function", }; // Act var attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Equal(12, attributes.Count); Assert.Equal("true", attributes["data-ajax"]); Assert.Equal("confirm", attributes["data-ajax-confirm"]); Assert.Equal("POST", attributes["data-ajax-method"]); Assert.Equal("#loadingElement", attributes["data-ajax-loading"]); Assert.Equal(450, attributes["data-ajax-loading-duration"]); Assert.Equal("http://someurl.com", attributes["data-ajax-url"]); Assert.Equal("#someId", attributes["data-ajax-update"]); Assert.Equal("before", attributes["data-ajax-mode"]); Assert.Equal("some_begin_function", attributes["data-ajax-begin"]); Assert.Equal("some_complete_function", attributes["data-ajax-complete"]); Assert.Equal("some_failure_function", attributes["data-ajax-failure"]); Assert.Equal("some_success_function", attributes["data-ajax-success"]); }
public void ToUnobtrusiveHtmlAttributesWithEmptyOptions() { // Arrange AjaxOptions options = new AjaxOptions(); // Act IDictionary<string, object> attributes = options.ToUnobtrusiveHtmlAttributes(); // Assert Assert.Single(attributes); Assert.Equal("true", attributes["data-ajax"]); }