MVC方法从ListBoxFor方法输出无序列表 [英] MVC Method to output unordered list from the ListBoxFor method

查看:346
本文介绍了MVC方法从ListBoxFor方法输出无序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC3,我想用Html.ListBoxFor方法来改变HTML输出,这样,而不是与所有可用值的HTML列表框(和选择的值高亮显示),我想简单地输出一个无序列表(UL ,所选的项目,而不是一个SELECT元素的LI)。问题是,我想保持的完全相同的方法签名作为ListBoxFor方法,接受MultiSelectList对象和列表这是选择的值。那么我想无序列表为UL / LI HTML输出仅所选项目的值(不是键)。这里是方法签名,我想有。这怎么可能实现呢?

 公共静态MvcHtmlString ListBoxForAsUnorderedList<的TModel,TProperty>
    (这与的HtmlHelper LT;的TModel>的HtmlHelper,
          防爆pression<&Func键LT;的TModel,TProperty>>前pression,
          IEnumerable的< SelectListItem>选择列表)


解决方案

我想通了这一点,并在code是在以下情况下,它可以帮助别人了。基本上,我传递的参数,以通用的ListBox方法,找回产生的原始的HTML,然后使用LINQ到XML抢只有选定的值转换为字符串数组。然后,我通过字符串数组和循环使用的TagBuilder(UL)来生成列表。我公开有关使用此方法的任何意见或批评。

 公共静态MvcHtmlString ListBoxForAsUnorderedList<的TModel,TProperty>(此的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,TProperty>>前pression,IEnumerable的&LT ; SelectListItem>选择列表)
{
  VAR mvcHtmlString = System.Web.Mvc.Html.SelectExtensions.ListBox(的HtmlHelper,防爆pressionHelper.GetEx pressionText(如pression),选择列表,NULL);
  。VAR selectedValues​​ = XDocument.Parse(mvcHtmlString.ToHtmlString())的后代(选项),其中(E =>(串)e.Attribute(选中)==选择)。选择(E =&GT ; e.Value).ToArray();
  var标记=新TagBuilder(UL);
  的foreach(在selectedValues​​ VAR值)
  {
    VAR itemTag =新TagBuilder(「李先生」);
    itemTag.SetInnerText(值);
    tag.InnerHtml + = itemTag.ToString();
  }
  返回新MvcHtmlString(tag.ToString());
}

In MVC3, I want to change the HTML output by the Html.ListBoxFor method so that instead of an HTML list box with all the available values (and the selected values highlighted), I want to simply output an unordered list (UL, LI) of the selected items and not a SELECT element. The issue is that I want to keep exactly the same method signature as the ListBoxFor method, accepting a MultiSelectList object and a List which is the selected values. I then want the unordered list to only output the selected item values (not keys) as UL/LI html. Here is the method signature I would like to have. How can this be accomplished?

public static MvcHtmlString ListBoxForAsUnorderedList <TModel, TProperty>
    (this HtmlHelper<TModel> htmlHelper, 
          Expression<Func<TModel, TProperty>> expression, 
          IEnumerable<SelectListItem> selectList)

解决方案

I figured this out and the code is below in case it helps anyone else out. Basically, I passed the parameters to the generic ListBox method, getting back the raw HTML generated and then used Linq to XML to grab only the "selected" values into a string array. I then looped through the string array and used a TagBuilder("ul") to generate the list. I am open to any comments or criticisms about using this method.

public static MvcHtmlString ListBoxForAsUnorderedList<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
{
  var mvcHtmlString = System.Web.Mvc.Html.SelectExtensions.ListBox(htmlHelper, ExpressionHelper.GetExpressionText(expression), selectList, null);
  var selectedValues = XDocument.Parse(mvcHtmlString.ToHtmlString()).Descendants("option").Where(e => (string)e.Attribute("selected") == "selected").Select(e => e.Value).ToArray();
  var tag = new TagBuilder("ul");
  foreach (var value in selectedValues)
  {
    var itemTag = new TagBuilder("li");
    itemTag.SetInnerText(value);
    tag.InnerHtml += itemTag.ToString();
  }
  return new MvcHtmlString(tag.ToString());
}

这篇关于MVC方法从ListBoxFor方法输出无序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆