从CheckboxList HTML Helper获取以逗号分隔的字符串 [英] Get comma-separated string from CheckboxList HTML Helper
问题描述
我从Internet上获得了CheckboxListFor
html helper扩展的以下代码.目前,它在SelectedValues
中从复选框列表中返回所选值的List<string>
.我想在SelectedValues
中获取一个逗号分隔的字符串.
I got the following code from the internet for CheckboxListFor
html helper extension. At the moment, in the SelectedValues
it is returning a List<string>
of selected values from the checkboxlist. I want to get a comma-separated string in SelectedValues
.
有人可以告诉我我如何实现吗?
Can anyone tell me how I can achieve it?
这是代码:
HTMLHelper扩展名:
HTMLHelper extension:
/// <summary>
/// Returns a checkbox for each of the provided <paramref name="items"/>.
/// </summary>
public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string listName, IEnumerable<SelectListItem> items, object htmlAttributes = null)
{
var container = new TagBuilder("div");
foreach (var item in items)
{
var label = new TagBuilder("label");
label.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
var cb = new TagBuilder("input");
cb.MergeAttribute("type", "checkbox");
cb.MergeAttribute("name", listName);
cb.MergeAttribute("value", item.Value ?? item.Text);
if (item.Selected)
cb.MergeAttribute("checked", "checked");
label.InnerHtml = cb.ToString(TagRenderMode.SelfClosing) + item.Text;
container.InnerHtml += label.ToString();
}
return new MvcHtmlString(container.ToString());
}
private static IEnumerable<SelectListItem> GetCheckboxListWithDefaultValues(object defaultValues, IEnumerable<SelectListItem> selectList)
{
var defaultValuesList = defaultValues as IEnumerable;
if (defaultValuesList == null)
return selectList;
IEnumerable<string> values = from object value in defaultValuesList
select Convert.ToString(value, CultureInfo.CurrentCulture);
var selectedValues = new HashSet<string>(values, StringComparer.OrdinalIgnoreCase);
var newSelectList = new List<SelectListItem>();
selectList.ForEach(item =>
{
item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
newSelectList.Add(item);
});
return newSelectList;
}
/// <summary>
/// Returns a checkbox for each of the provided <paramref name="items"/>.
/// </summary>
public static MvcHtmlString CheckBoxListFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression,
IEnumerable<SelectListItem> items, object htmlAttributes = null)
{
var listName = ExpressionHelper.GetExpressionText(expression);
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
items = GetCheckboxListWithDefaultValues(metaData.Model, items);
return htmlHelper.CheckBoxList(listName, items, htmlAttributes);
}
在视图中:
@Html.CheckBoxListFor(model => model.SelectedValues, Model.MySelectList)
型号:
public class MyViewModel
{
public SelectList MySelectList{ get; set; }
public List<string> SelectedValues{ get; set; }
//public string SelectedValues{ get; set; } Can I get comma separated string here
}
请注意,我需要从帮助程序返回的逗号分隔的字符串,而不是在获得控制器动作的列表之后.
为什么要尝试执行此操作?:
//Here in my model, I am getting `SelectedValues` which is a List<String>.
public ActionResult Index(MyViewModel model)
{
//My code....
}
可见
//But I cannot save this list into RouteValueDictionary like:
var searchCriteria = new RouteValueDictionary();
searchCriteria["model.SelectedValues"] = Model.SelectedValues; // List<string> cannot be save here. That's why I needed comma separated string.
var viewDataDictionary = new ViewDataDictionary();
viewDataDictionary["searchCriteria"] = searchCriteria;
@Html.Partial("_MyPagingView", Model.MyList, viewDataDictionary)
_MyPagingView
内部有一个完整的机制,每当单击下一页时,该机制就会调用Index
Action.为了保持搜索状态,我们需要将搜索到的数据保留在RouteValueDictionary
中.
There is a whole mechanism inside the _MyPagingView
which calls Index
Action whenever next page is clicked. And to preserve the state of the search we need to keep our searched data inside RouteValueDictionary
.
推荐答案
您可以创建一个辅助方法以将SelectedValues
添加到a RouteValueDictionary
You could create a helper method to add theSelectedValues
to aRouteValueDictionary
public void AddRoutes(List<string> values, string propertyName, RouteValueDictionary dictionary)
{
for (int i = 0; i < values.Count; i++ )
{
string key = string.Format("{0}[{1}]", propertyName, i);
dictionary[key] = values[i];
}
}
,然后将其用作
var searchCriteria = new RouteValueDictionary();
AddRoutes(Model.SelectedValues, "SelectedValues", searchCriteria);
并避免需要创建隐藏的输入并使用javascript
and avoid the need to create a hidden input and use javascript
这篇关于从CheckboxList HTML Helper获取以逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!