[英]LabelFor not working as expected
I am trying to create labels dynamically according to the number of items in a collection. 我试图根据集合中项目的数量动态创建标签。
My model is passed to my view fine, however when iterating through the collection - i can't get the labels to display the values of the list of string. 我的模型很好地传递给了我的视图,但是在遍历集合时-我无法获得用于显示字符串列表值的标签。
For example i am using the following code: 例如我正在使用以下代码:
@foreach (var ac in model.myClass.listofstring)
{
@Html.LabelFor(a => ac)
}
If the collection has 4 items, the above code will output "acacacac" I would like it to output 4 labels, with the string values of the items in the list. 如果集合有4个项目,则上面的代码将输出“ acacacac”,我希望它输出4个标签,并在列表中显示这些项目的字符串值。
I have also tried @Html.LabelFor(a => ac.ToString())
and @Html.LabelFor(a => ac.ToString)
but the view then doesn't render. 我也尝试了
@Html.LabelFor(a => ac.ToString())
和@Html.LabelFor(a => ac.ToString)
但视图随后未呈现。
I know the values are in the list as i can access them in my controller. 我知道值在列表中,因为我可以在控制器中访问它们。 The value of
test
is what i want in my view. 我认为
test
的价值是我想要的。
foreach (var ac in myClass.listofstring)
{
var test = ac.ToString();
}
I have also tried this, with no luck: 我也尝试过这个,没有运气:
@for (var i = 0; i < model.myClass.listofstring.Count(); i++)
{
@Html.LabelFor(a => a[i])
}
Label isn't displaying the value of the item, it's just displaying its name. 标签不显示项目的值,而只是显示其名称。 Use @Html.DisplayFor instead
使用@ Html.DisplayFor代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.