Asp.net MVC Preview 4 中自定义Jquery的HtmlHelper扩展
来源:http://http://www.zjjv.com//blogs.com/chsword/archive/2008/07/22/asp_netmvcpreview4jqueryhelper.html
前两天介绍了mvc pv4中AjaxHelper的用法
的确是比较方便,不过也有其缺点,那就是并不是所有人都用Asp.net Ajax的,所以这样做还是显示出了ms的霸气
没办法,工程里都用的是JQuery于是先试写了2个小功能
先说使用方法吧,第一个功能是Ajax载入另个页面
<div>
</div>
<%=Html.JLoadLink<MvcApplication1.Controllers.HomeController>(c => c.Ajax(), "Ajax载入", new AjaxOptions() {
UpdateTargetId="as"
}) %>
最后生成的是一个连接,一点,就将HomeController.Ajax()载入到as这个div中了
另一个扩展呢,是一个简单的toggle功能
就是一个元素,原来显示就让它隐藏,原来隐藏就让他显示,不过还是挺多地方用到的
<%=Html.JToggle("显示/隐藏","d1")%>
<div>
天之道 损有余 而补不足<br />
是故虚胜实 不足胜有余<br />
</div>
或
<%=Html.JToggle("显示/隐藏","#d1")%>
<div>
天之道 损有余 而补不足<br />
是故虚胜实 不足胜有余<br />
</div>
当然,fade也可以,那样就漂亮了许多了
好,步入正题 ,代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Web.Routing;
namespace System.Web.Mvc {
/// <summary>
/// http://http://www.zjjv.com//blogs.com
/// 重典
/// QQ:77298666
/// </summary>
static public class AjaxJquery {
/// <summary>
/// 就是一个元素,原来显示就让它隐藏,原来隐藏就让他显示
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
static public String JToggle(this HtmlHelper a, string linkText, String ToggleID) {
return a.JToggle( linkText, ToggleID, null);
}
/// <summary>
/// 就是一个元素,原来显示就让它隐藏,原来隐藏就让他显示
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
static public String JToggle(this HtmlHelper a, string linkText, String ToggleID, object htmlAttributes) {
string linkFormat = "<a href="{0}" {1} {3}>{2}</a>";
string atts = string.Empty;
string ajaxs = string.Empty;
if (htmlAttributes != null)//由object 序列成key="value" 这样的属性模式
atts = AjaxJquery.ConvertObjectToAttributeList(htmlAttributes);
ajaxs = string.Format("onclick="$('{0}').toggle(); " ",
ToggleID[0] == '#' ? ToggleID : "#" + ToggleID//Jquery里id不是要#嘛得验一下个人的那啥不同啊
);
string result = string.Format(CultureInfo.InvariantCulture, linkFormat, "#", atts, a.Encode(linkText), ajaxs);
return result;
}
/// <summary>
/// 生成一个可以加载地址的Ajax连接
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
static public string JLoadLink<T>(this HtmlHelper a, System.Linq.Expressions.Expression<Action<T>> action, string linkText, AjaxOptions ajaxoptions) where T : Controller {
return a.JLoadLink<T>(action, linkText, null, ajaxoptions);
}
/// <summary>
/// 生成一个可以加载地址的Ajax连接
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
static public string JLoadLink<T>(this HtmlHelper a, System.Linq.Expressions.Expression<Action<T>> action, string linkText, object htmlAttributes, AjaxOptions ajaxoptions) where T : Controller {
string linkFormat = "<a href="{0}" {1} {3}>{2}</a>";
string atts = string.Empty;
string ajaxs = string.Empty;
if (htmlAttributes != null)
atts = AjaxJquery.ConvertObjectToAttributeList(htmlAttributes);
string link = LinkBuilder.BuildUrlFromExpression<T>(a.ViewContext, action);
//* */a.BuildUrlFromExpression<T>(action);
if (ajaxoptions != null) {
ajaxs = string.Format("onclick="{0}$ActionLink('{1}','{2}'); return false; " ",
ajaxoptions.OnBegin,//$ActionLink这个方法定义在framework.js里,可以看一下
link,
ajaxoptions.UpdateTargetId
);
}
string result = string.Format(CultureInfo.InvariantCulture, linkFormat, "#", atts, a.Encode(linkText), ajaxs);
return result;
}
/// <summary>
/// 从MVC工程里搞出来的,原来有程序集保护用不了。
/// </summary>
/// <param></param>
/// <returns></returns>
public static string ConvertObjectToAttributeList(object value) {
StringBuilder sb = new StringBuilder();
if (value != null) {
IDictionary<string, object> d = value as IDictionary<string, object>;
if (d == null) {
d = new RouteValueDictionary(value);
}
string resultFormat = "{0}="{1}" ";
foreach (string attribute in d.Keys) {
object thisValue = d[attribute];
if (d[attribute] is bool) {
thisValue = d[attribute].ToString().ToLowerInvariant();
}
sb.AppendFormat(resultFormat, attribute.Replace("_", "").ToLowerInvariant(), thisValue);
}
}
return sb.ToString();
}
}
}