码迷,mamicode.com
首页 > Web开发 > 详细

mvc4扩展方法

时间:2016-11-28 00:26:09      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:val   sum   制作   end   linq   html   exp   orm   returns   

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

 

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=‘{1}‘>{1}</input>", 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扩展方法

标签:val   sum   制作   end   linq   html   exp   orm   returns   

原文地址:http://www.cnblogs.com/lunawzh/p/6107639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!