Go to the documentation of this file.00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Web.Mvc;
00006 using System.Web.Mvc.Html;
00007 using System.Web.Routing;
00008
00009 namespace N2.Web.Mvc.Html
00010 {
00011 public static class AjaxExtensions
00012 {
00013 public static IDisposable BeginAsyncAction(this HtmlHelper helper, string actionName)
00014 {
00015 return helper.BeginAsyncAction(actionName, new RouteValueDictionary());
00016 }
00017
00018 public static IDisposable BeginAsyncAction(this HtmlHelper helper, string actionName, object routeValues)
00019 {
00020 return helper.BeginAsyncAction(actionName, new RouteValueDictionary(routeValues));
00021 }
00022
00023 public static IDisposable BeginAsyncAction(this HtmlHelper helper, string actionName, RouteValueDictionary routeValues)
00024 {
00025 var id = "async" + Guid.NewGuid().ToString().Replace('-', '_');
00026 var url = new UrlHelper(helper.ViewContext.RequestContext).Action(actionName, routeValues);
00027
00028 var tag = new System.Web.Mvc.TagBuilder("div");
00029 tag.Attributes["id"] = id;
00030 tag.AddCssClass("async loading");
00031
00032 helper.ViewContext.Writer.Write(tag.ToString(TagRenderMode.StartTag));
00033
00034 var asyncLoadScript = string.Format(@"<script type='text/javascript'>//<![CDATA[
00035 jQuery(document).ready(function(){{jQuery('#{0}').load('{1}');}});//]]></script>", id, url);
00036
00037 var end = tag.ToString(TagRenderMode.EndTag) + asyncLoadScript;
00038
00039 return new WritesOnDispose(helper, end);
00040 }
00041
00042 class WritesOnDispose : IDisposable
00043 {
00044 HtmlHelper helper;
00045 string endHtml;
00046
00047 public WritesOnDispose(HtmlHelper helper, string htmlToWrite)
00048 {
00049 this.helper = helper;
00050 this.endHtml = htmlToWrite;
00051 }
00052
00053 #region IDisposable Members
00054
00055 public void Dispose()
00056 {
00057 helper.ViewContext.Writer.Write(endHtml);
00058 }
00059
00060 #endregion
00061 }
00062 }
00063 }