首页 > 代码库 > mvc4扩展方法

mvc4扩展方法

制作扩展方法,方便网页中使用,下面做了两个例子

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;

namespace System.Web.Mvc
{
    public static class CheckBoxListHelper
    {
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string[] showStr,string attrName, bool isHorizon = true)
        {
            if (showStr == null)
                return null;
            if (attrName == null)
            {
                attrName = "temp";
            }
            StringBuilder sb = new StringBuilder();
            int i=0;
            foreach (var item in showStr)
            {
                i++;
                sb.Append("<label class=‘checkbox-inline‘>");
                sb.Append(string.Format("<input type=‘checkbox‘ id=‘{0}‘ name=‘{0}‘ value=http://www.mamicode.com/‘{1}‘>{1}", attrName + i, item));
                sb.Append("</label>");
            }
            return new MvcHtmlString(sb.ToString());
        }
        /// <summary>
        /// 截取字符长度
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="showStr"></param>
        /// <param name="attrName"></param>
        /// <param name="isHorizon"></param>
        /// <returns></returns>
        public static MvcHtmlString GetStringSub(this HtmlHelper helper, string s, int len)
        {
            if (string.IsNullOrEmpty(s))
                return new MvcHtmlString("");
            if (s.Length <= len)
                return new MvcHtmlString(s.ToString());
            else
            {
                return new MvcHtmlString("<span title=‘" + s + "‘>" + s.Substring(0, len) + ".</span>");
            }
        }
    }
}

 

mvc4扩展方法