代码改变世界

mvc自定义扩展控件

2012-11-24 22:28  甘超波  阅读(234)  评论(0编辑  收藏  举报

 

注意点 自定义类必须在System.Web.Mvc 类必须为静态扩展类.

namespace System.Web.Mvc
{
public static class MyFormLable
{
public static MvcHtmlString GetForm<T>(this HtmlHelper Help, T Model)//要定义泛型,在方法中指定泛型
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(@" <fieldset>");
sb.AppendLine(@" <legend>Fields</legend>");
foreach (PropertyInfo model in Model.GetType().GetProperties())
{

sb.AppendLine(" <div class=\"editor-label\">");
sb.AppendLine("<label for=\""+model.Name +"\">"+model .Name +"</label>");
sb.AppendLine("</div>");
sb.AppendLine("<div class=\"editor-field\">");

sb.AppendLine(" <input id=\""+model .Name +"\" name=\""+model .Name +"\" type=\"text\" value=\"\" />");
sb.AppendLine("<span class=\"field-validation-valid\" id=\"ID_validationMessage\"></span>");
sb.AppendLine("</div>");

}
sb.AppendLine(@" </fieldset>");
//sb.AppendLine(@" <% } %>");
return MvcHtmlString.Create(sb.ToString());

}
public static MvcHtmlString PageSize(this HtmlHelper help, int? count,string actionName,string controller)
{
if (count == null) return null;
StringBuilder sb = new StringBuilder();
sb.AppendLine("<div>");
for (int i = 0; i < count; i++)
{
sb.Append(" <a href=\"/" + controller + "/" + actionName + "?PageIndex=" + (i + 1) + "\">" + (i + 1) + "</a>");
}
sb.AppendLine("</div>");
return MvcHtmlString.Create(sb.ToString());

}
}
}