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

htmlExtensions类

时间:2018-05-31 17:22:24      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:ado   code   mat   drop   stat   lsh   contains   self   havel   

//在th中根据排序方向生成拍序列的展示标记
public static MvcHtmlString orderColumn(this HtmlHelper helper, string orderField, string orderWay, string fieldName)
{
  StringBuilder stringBuilder = new StringBuilder();
  if (fieldName == orderField)
  {
    IDictionary<string, object> HtmlAttributes = new Dictionary<string, object>();
    HtmlAttributes.Add("height", "10px");
    if (orderWay == "asc")
      HtmlAttributes.Add("src", "/Content/images/desc.png");
    if (orderWay == "desc")
      HtmlAttributes.Add("src", "/Content/images/asc.png");

    TagBuilder tagBuilder = new TagBuilder("img");
    tagBuilder.MergeAttributes<string, object>(HtmlAttributes);
    string inputAllHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
    stringBuilder.Append(inputAllHtml);
  }
  return MvcHtmlString.Create(stringBuilder.ToString());
}

//多选下拉框
public static MvcHtmlString DropDownListMul(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList,object htmlAttributes = null)
{
  StringBuilder stringBuilder = new StringBuilder();
  //select属性
  IDictionary<string, object> selectTagAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
  selectTagAttributes.Add("name", name);
  selectTagAttributes.Add("multiple", "multiple");
  //select标记,并附加属性
  TagBuilder selectTag = new TagBuilder("select");
  selectTag.MergeAttributes<string, object>(selectTagAttributes);

  //添加select开始标记
  stringBuilder.Append(selectTag.ToString(TagRenderMode.StartTag));
  //添加option标记
  foreach (var item in selectList)
  {
    IDictionary<string, object> optionTagAttributes = new Dictionary<string, object>();
    optionTagAttributes.Add("value", item.Value);
    if (item.Selected)
      optionTagAttributes.Add("selected", "selected");
    TagBuilder optionTag = new TagBuilder("option");
    optionTag.MergeAttributes<string, object>(optionTagAttributes);

    stringBuilder.Append(optionTag.ToString(TagRenderMode.StartTag));
    stringBuilder.Append(item.Text);
    stringBuilder.Append(optionTag.ToString(TagRenderMode.EndTag));
  }
  //添加select结束标记
  stringBuilder.Append(selectTag.ToString(TagRenderMode.EndTag));
  return MvcHtmlString.Create(stringBuilder.ToString());
}

//只读文本框(不可变更)
public static MvcHtmlString TextBoxRead(this HtmlHelper helper, string name, object value, object htmlAttributes = null)
{
  //获取传入的htmlAttributes信息
  IDictionary<string, object> HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
  //构建checkbox属性
  HtmlAttributes.Add("type", "text");
  HtmlAttributes.Add("name", string.Format("{0}", name));
  HtmlAttributes.Add("class", "labelShow");
  HtmlAttributes.Add("readonly", "true");
  HtmlAttributes.Add("value", value);

  TagBuilder tagBuilder = new TagBuilder("input");
  tagBuilder.MergeAttributes<string, object>(HtmlAttributes);
  string inputAllHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
  return MvcHtmlString.Create(inputAllHtml);
}

//带for的单选列表
public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> helper,Expression<Func<TModel, TProperty>> expression,IEnumerable<SelectListItem> selectList, object htmlAttributes = null, bool isHorizon = true)
{
  string[] propertys = expression.ToString().Split(".".ToCharArray());
  string name = string.Join(".", propertys, 1, propertys.Length - 1);

  return RadioButtonList(helper, name, selectList, htmlAttributes, isHorizon);
}

//单选列表基础类
public static MvcHtmlString RadioButtonList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes = null, bool isHorizon = true)
{
  StringBuilder stringBuilder = new StringBuilder();
  foreach (SelectListItem selectItem in selectList)
  {
    //获取传入的htmlAttributes信息
    IDictionary<string, object> HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    //构建checkbox属性
    HtmlAttributes.Add("type", "radio");
    HtmlAttributes.Add("id", string.Format("{0}[{1}]", name, selectItem.Value));
    HtmlAttributes.Add("name", string.Format("{0}", name));
    HtmlAttributes.Add("class", "ruiCheckBox");
    HtmlAttributes.Add("value", selectItem.Value);
    if (selectItem.Selected)
    {
      HtmlAttributes.Add("checked", "checked");
    }
    TagBuilder tagBuilder = new TagBuilder("input");
    tagBuilder.MergeAttributes<string, object>(HtmlAttributes);
    string inputAllHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
    string containerFormat = isHorizon ? @"<span>{0}{1}</span>&nbsp;&nbsp;" : @"<p><span>{0}{1}</span></p>";
    stringBuilder.AppendFormat(containerFormat, selectItem.Text, inputAllHtml);
  }
  return MvcHtmlString.Create(stringBuilder.ToString());
}

//带for的 复选列表
public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes=null, bool isHorizon = true)
{
  string[] propertys = expression.ToString().Split(".".ToCharArray());
  string name = string.Join(".", propertys, 1, propertys.Length - 1);

  return CheckBoxList(helper, name, selectList, htmlAttributes, isHorizon);
}

//复选列表基础类
/// <summary>
///
/// </summary>
/// <param name="helper"></param>
/// <param name="name">标记名称</param>
/// <param name="selectList">列表项</param>
/// <param name="htmlAttributes">html属性</param>
/// <param name="isHorizon">排列方式</param>
/// <returns></returns>
public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes=null, bool isHorizon = true)
{
  StringBuilder stringBuilder = new StringBuilder();
  foreach (SelectListItem selectItem in selectList)
  {
    //获取传入的htmlAttributes信息
    IDictionary<string, object> HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    //构建checkbox属性
    HtmlAttributes.Add("type", "checkbox");
    HtmlAttributes.Add("id", string.Format("{0}[{1}]", name, selectItem.Value));
    HtmlAttributes.Add("name", string.Format("{0}", name));
    HtmlAttributes.Add("class", "ruiCheckBox");
    HtmlAttributes.Add("value", selectItem.Value);
    if (selectItem.Selected)
    {
      HtmlAttributes.Add("checked", "checked");
    }
    TagBuilder tagBuilder = new TagBuilder("input");
    tagBuilder.MergeAttributes<string, object>(HtmlAttributes);
    string inputAllHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
    string containerFormat = isHorizon ? @"<span>{0}{1}</span>" : @"<p><span>{0}{1}</span></p>";
    stringBuilder.AppendFormat(containerFormat, selectItem.Text, inputAllHtml);
  }
  return MvcHtmlString.Create(stringBuilder.ToString());
}

//复选框辅助类 - 授权页面用
/// <summary>
///
/// </summary>
/// <param name="helper"></param>
/// <param name="name">标记名称</param>
/// <param name="allowOperations">允许出现的项目</param>
/// <param name="haveOperations">已选中的项目</param>
/// <param name="allOperations">所有的列表项信息,用来获取操作名称的</param>
/// <returns></returns>
public static MvcHtmlString CheckBoxListPriv(this HtmlHelper helper, string name, string allowOperations, string haveOperations,
IEnumerable<SelectListItem> allOperations)
{
  StringBuilder stringBuilder = new StringBuilder();
  List<string> allowList = (from a in allowOperations.Split(‘,‘)
               where a.Length > 0
               select a).ToList();
  List<string> haveList = (from a in haveOperations.Split(‘,‘)
              where a.Length > 0
              select a).ToList();
  foreach (string code in allowList)
  {
    //获取传入的htmlAttributes信息
    IDictionary<string, object> HtmlAttributes = new Dictionary<string, object>();
    //构建checkbox属性
    HtmlAttributes.Add("type", "checkbox");
    HtmlAttributes.Add("name", name);
    HtmlAttributes.Add("class", "ruiCheckBox");
    HtmlAttributes.Add("value", code);
    if (haveList.Contains(code))
    {
      HtmlAttributes.Add("checked", "checked");
    }
    TagBuilder tagBuilder = new TagBuilder("input");
    tagBuilder.MergeAttributes<string, object>(HtmlAttributes);
    string inputAllHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
    string containerFormat = string.Format(@"<span>{0}{1}</span>", HtmlExtensions.getNameByCode(allOperations, code), inputAllHtml);
    stringBuilder.Append(containerFormat);
  }
  return MvcHtmlString.Create(stringBuilder.ToString());
}

//复选框辅助类 - 多选
/// <summary>
///
/// </summary>
/// <param name="helper"></param>
/// <param name="name">标记名称</param>
/// <param name="selectedItems">已选中的项目,分割</param>
/// <param name="allItems">所有的项目</param>
/// <returns></returns>
public static MvcHtmlString checkBoxListHelper(this HtmlHelper helper, string name, string selectedItems, IEnumerable<SelectListItem> allItems)
{
  StringBuilder stringBuilder = new StringBuilder();
  List<string> allowList = (from a in selectedItems.Split(‘,‘)
               where a.Length > 0
               select a).ToList();
  foreach (SelectListItem item in allItems)
  {
    //获取传入的htmlAttributes信息
    IDictionary<string, object> HtmlAttributes = new Dictionary<string, object>();
    //构建checkbox属性
    HtmlAttributes.Add("type", "checkbox");
    HtmlAttributes.Add("name", name);
    HtmlAttributes.Add("class", "ruiCheckBox");
    HtmlAttributes.Add("value", item.Value);
    if (allowList.Contains(item.Value))
    {
      HtmlAttributes.Add("checked", "checked");
    }
    TagBuilder tagBuilder = new TagBuilder("input");
    tagBuilder.MergeAttributes<string, object>(HtmlAttributes);
    string inputAllHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
    string containerFormat = string.Format(@"<span>{0}{1}</span>", item.Text, inputAllHtml);
    stringBuilder.Append(containerFormat);
  }
  return MvcHtmlString.Create(stringBuilder.ToString());
}

/// <summary>
/// 显示字段的名称DisplayName的值
/// 好了之后,统一修改为@Html.DisplayForDate
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="html"></param>
/// <param name="expression"></param>
/// <returns></returns>
public static MvcHtmlString ShowDate<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
  var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
  //DateTime类型
  if (typeof(TValue) == typeof(DateTime))
  {
    if (metadata.ModelType == typeof(DateTime))
    {
      DateTime dt = (DateTime)metadata.Model;
      return MvcHtmlString.Create(dt.ToString(rui.config.dateFormat));
    }
    return MvcHtmlString.Create("Oops,not a DateTime");
  }
  //DateTime?类型
  else if (typeof(TValue) == typeof(DateTime?))
  {
    if (metadata.ModelType == typeof(DateTime?))
    {
      if (metadata.Model == null)
        return MvcHtmlString.Create("");
      else
      {
        DateTime dt = Convert.ToDateTime(metadata.Model);
        return MvcHtmlString.Create(dt.ToString(rui.config.dateFormat));
      }
    }
    return MvcHtmlString.Create("Oops,not a DateTime");
  }
  else
  {
    return MvcHtmlString.Create("Oops,not a DateTime");
  }
}

//返回列表项中某个Code的Name值
private static string getNameByCode(IEnumerable<SelectListItem> list, string code)
{
  string result = "";
  foreach (var item in list)
  {
    if (item.Value == code)
      return item.Text;
  }
  return result;
}

htmlExtensions类

标签:ado   code   mat   drop   stat   lsh   contains   self   havel   

原文地址:https://www.cnblogs.com/feihusurfer/p/9117662.html

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