asp.net mvc Ajax 扩展
虽然 mvc 的 Ajax 扩展已经基本够用了。但是 遇到遇到一些特殊的情况,Ajax还是不能用。比如
<a href='#.'> <i class='icon'></i> 提交</a>
就是中间带HTML 。
出于这个考虑,把 ajax options 提取出来。
public static class AjaxExtensions { public static IHtmlString AjaxOptions(this AjaxHelper ajaxHelper, AjaxOptions ajaxOptions) { IDictionary<string, object> attributes = new Dictionary<string, object>(); if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { var ajaxAttributes = ajaxOptions.ToUnobtrusiveHtmlAttributes(); foreach (var attribute in ajaxAttributes) { attributes[attribute.Key] = attribute.Value; } } else { attributes["onclick"] = GenerateAjaxScript(ajaxOptions, "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});"); } return new HtmlString(string.Join(" ", attributes.Select(t => string.Format("{0}=\"{1}\"", t.Key, t.Value)))); } private static string GenerateAjaxScript(AjaxOptions ajaxOptions, string scriptFormat) { StringBuilder stringBuilder = new StringBuilder("{ "); switch (ajaxOptions.InsertionMode) { case InsertionMode.Replace: stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " insertionMode: {0},", "Sys.Mvc.InsertionMode.replace"); break; case InsertionMode.InsertBefore: stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " insertionMode: {0},", "Sys.Mvc.InsertionMode.insertBefore"); break; case InsertionMode.InsertAfter: stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " insertionMode: {0},", "Sys.Mvc.InsertionMode.insertAfter"); break; default: stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " insertionMode: {0},", ((int)ajaxOptions.InsertionMode).ToString(CultureInfo.InvariantCulture)); break; } stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("confirm", ajaxOptions.Confirm)); stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("httpMethod", ajaxOptions.HttpMethod)); stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("loadingElementId", ajaxOptions.LoadingElementId)); stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("updateTargetId", ajaxOptions.UpdateTargetId)); stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("url", ajaxOptions.Url)); stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onBegin", ajaxOptions.OnBegin)); stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onComplete", ajaxOptions.OnComplete)); stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onFailure", ajaxOptions.OnFailure)); stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onSuccess", ajaxOptions.OnSuccess)); stringBuilder.Length--; stringBuilder.Append(" }"); return string.Format(CultureInfo.InvariantCulture, scriptFormat, stringBuilder.ToString()); } private static string PropertyStringIfSpecified(this AjaxOptions options, string propertyName, string propertyValue) { if (!string.IsNullOrEmpty(propertyValue)) { string text = propertyValue.Replace("'", "\\'"); return string.Format(CultureInfo.InvariantCulture, " {0}: '{1}',", propertyName, text); } return string.Empty; } private static string EventStringIfSpecified(this AjaxOptions options, string propertyName, string handler) { if (!string.IsNullOrEmpty(handler)) { return string.Format(CultureInfo.InvariantCulture, " {0}: Function.createDelegate(this, {1}),", propertyName, handler.ToString()); } return string.Empty; } }
https://git.oschina.net/PassingWind/codes/ycsmjvi1d430gnl5uobhe71
Categories:
.net
Tags:
asp.net mvc
已禁用评论。