剃刀CheckBoxFor的名单,LT;字符串&GT; [英] Razor CheckBoxFor on List<string>
本文介绍了剃刀CheckBoxFor的名单,LT;字符串&GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎无法弄清楚如何获得一个字符串列表在我的形式复选框来呈现。试图从不同的地点和问题/答案的几件事情就到这里,没有运气。
MODEL
VAR模型=新ProjectModel()
{
项目类型=新的List&LT;串GT;()
{
品牌创造,
网页设计,
平面设计,
自定义编程,
电子商务,
其他
},
};
查看 - 我已经尝试以下方法
@foreach(在Model.ProjectType VAR项)
{
@ Html.CheckBoxFor(型号=&GT;真,项)
&LT; BR /&GT;
} @foreach(在Model.Budget VAR项)
{
@ Html.CheckBox(项目)
&LT; BR /&GT;
}
解决方案
我宁愿用枚举显示复选框。我知道这是不是你的答案,但你可以考虑这样做。
您需要做的这些步骤>
-
枚举
[国旗]
公共枚举项目类型
{
[显示(NAME =品牌创造)
BrandCreation = 1,
[显示(NAME =网页设计)]
网站设计= 2
} -
创建HTML扩展方法
公共静态IHtmlString CheckboxListForEnum&LT; T&GT;(这个HTML的HtmlHelper,字符串名称,T modelItems)其中T:结构
{
StringBuilder的SB =新的StringBuilder(); VAR displayAttributeType = typeof运算(DisplayAttribute); 的foreach()(在Enum.GetValues(typeof运算(T))笔项目投&LT;; T&GT)
{
字段信息字段= item.GetType()getfield命令(item.ToString())。 DisplayAttribute ATTRS =(DisplayAttribute)字段。
GetCustomAttributes(displayAttributeType,FALSE).FirstOrDefault(); TagBuilder建设者=新TagBuilder(输入);
长targetValue = Convert.ToInt64(项目);
长flagValue = Convert.ToInt64(modelItems); 如果((targetValue&安培; flagValue)== targetValue)
builder.MergeAttribute(选中,检查); builder.MergeAttribute(类型,复选框);
builder.MergeAttribute(价值,attrs.GetName());
builder.MergeAttribute(名,名);
builder.InnerHtml = attrs.GetName(); sb.Append(builder.ToString(TagRenderMode.Normal));
} 返回新HtmlString(sb.ToString());
} -
最后,在您的视图
@ Html.CheckboxListForEnum(项目类型,@ Model.ProjectType)
-
获取贴值
公众的ActionResult指数(的FormCollection形式)
{
字符串[] = AllStrings形式[项目类型]斯普利特('')。 的foreach(在AllStrings字符串项)
{
int值= int.Parse(项目);
}
}
I can't seem to figure out how to get a list of strings to render in my form as checkboxes. Tried a few things from various sites and questions/answers on here with no luck.
MODEL
var model = new ProjectModel()
{
ProjectType = new List<string>()
{
"Brand Creation",
"Web Design",
"Graphic Design",
"Custom Programming",
"E-Commerce",
"Other"
},
};
VIEW - I've tried the following ways.
@foreach (var item in Model.ProjectType)
{
@Html.CheckBoxFor(model => true, item)
<br />
}
@foreach (var item in Model.Budget)
{
@Html.CheckBox(item)
<br />
}
解决方案
I would rather use Enum to display checkboxes. I know it's not your answer but you may consider doing this.
You have to do these step >
Enum
[Flags] public enum ProjectType { [Display(Name = "Brand Creation")] BrandCreation = 1, [Display(Name = "Web Design")] WebDesign = 2 }
Create Html Extension method
public static IHtmlString CheckboxListForEnum<T>(this HtmlHelper html, string name, T modelItems) where T : struct { StringBuilder sb = new StringBuilder(); var displayAttributeType = typeof(DisplayAttribute); foreach (T item in Enum.GetValues(typeof(T)).Cast<T>()) { FieldInfo field = item.GetType().GetField(item.ToString()); DisplayAttribute attrs = (DisplayAttribute)field. GetCustomAttributes(displayAttributeType, false).FirstOrDefault(); TagBuilder builder = new TagBuilder("input"); long targetValue = Convert.ToInt64(item); long flagValue = Convert.ToInt64(modelItems); if ((targetValue & flagValue) == targetValue) builder.MergeAttribute("checked", "checked"); builder.MergeAttribute("type", "checkbox"); builder.MergeAttribute("value", attrs.GetName()); builder.MergeAttribute("name", name); builder.InnerHtml = attrs.GetName(); sb.Append(builder.ToString(TagRenderMode.Normal)); } return new HtmlString(sb.ToString()); }
Finally in your View
@Html.CheckboxListForEnum("ProjectType", @Model.ProjectType)
Get posted value
public ActionResult Index(FormCollection form) { string[] AllStrings = form["ProjectType"].Split(','); foreach (string item in AllStrings) { int value = int.Parse(item); } }
这篇关于剃刀CheckBoxFor的名单,LT;字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文