LabelFor环路不工作(用于/的foreach /模板) [英] LabelFor not working in loop (for/foreach/template)
问题描述
我试图做一个复选框列表,但Html.LabelFor不工作。
它使生成<标签=>有的文字< /标签>
,注意空为
属性
I'm trying to make a list of checkboxes but the Html.LabelFor is not working.
It keeps generating <label for="">Some text</label>
, note the empty For
attribute.
我试过并foreach循环之前,我试图用一个显示模板,但他们都存在这个问题。
我不相信手写输入字段是唯一的解决方案。
所有帮助/提示是多少AP preciated。
I've tried a for and foreach loop before I tried using a display template but they all have this problem. I refuse to believe that writing the input fields by hand is the only solution. All help/tips are much appreciated.
public class MyViewModel
{
public string Name { get; set; }
public bool Selected { get; set; }
}
控制器:
public ActionResult MyController()
{
var model = new List<MyViewModel>();
model.Add(new MyViewModel() { Name= "Foo" });
model.Add(new MyViewModel() { Name= "Bar" });
return View(model);
}
视图:
@model IEnumerable<MyViewModel>
<div>
@Html.DisplayFor(x => Model)
</div>
的显示模板:
@model MyViewModel
<p>
@Html.LabelFor(model => model.Selected, Model.Name)
@Html.EditorFor(model => model.Selected)
</p>
结果:
<div>
<p>
<label for="">Foo</label>
<input class="check-box" data-val="true" data-val-required="The Foo field is required."
name="[0].Selected" type="checkbox" value="true" />
<input name="[0].Selected" type="hidden" value="false" />
</p>
<p>
<label for="">Bar</label>
<input class="check-box" data-val="true" data-val-required="The Bar field is required."
name="[1].Selected" type="checkbox" value="true" />
<input name="[1].Selected" type="hidden" value="false" />
</p>
<div>
正如你所看到的&LT;标签&gt;
未如预期运行
推荐答案
的事情是,因为在你的主视图中直接有的IEnumerable&LT; MyViewModel&GT;
中,preFIX是 [0] .Selected
该助手不喜欢。您还会注意到,您的复选框没有一个ID。下面是你可以采用改变这种preFIX黑客攻击:
The thing is that since in your main view you directly have IEnumerable<MyViewModel>
, the prefix is [0].Selected
which the helpers doesn't like. You will also notice that your checkbox doesn't have an id. Here's a hack that you could employ to change this prefix:
@model MyViewModel
@{
ViewData.TemplateInfo.HtmlFieldPrefix = "Model" + ViewData.TemplateInfo.HtmlFieldPrefix;
}
<p>
@Html.LabelFor(model => model.Selected, Model.Name)
@Html.EditorFor(model => model.Selected)
</p>
显然,这个假定您的POST操作参数就叫做模式
:
[HttpPost]
public ActionResult Index(IEnumerable<MyViewModel> model) { ... }
这可能不总是这样的情况。
which might not always be the case.
另外请注意,而不是 @ Html.DisplayFor(X =&GT;产品型号)
在主视图中您可以使用 @ Html.EditorForModel( )
。
Also note that instead of @Html.DisplayFor(x => Model)
in your main view you could use @Html.EditorForModel()
.
另一种可能性是一个集合属性来包装这个在视图模型:
Another possibility is to wrap this in a view model with a collection property:
公共类MyViewModel
{
公共IEnumerable的项目{搞定;组; }
}
public class MyViewModel { public IEnumerable Items { get; set; } }
其中, ItemViewModel
:
public class ItemViewModel
{
public string Name { get; set; }
public bool Selected { get; set; }
}
,然后你的控制器:
and then your controller:
public ActionResult Index()
{
var model = new MyViewModel
{
Items = new[]
{
new ItemViewModel() { Name= "Foo" },
new ItemViewModel() { Name= "Bar" }
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
...
}
和视图:
@model MyViewModel
<div>
@using (Html.BeginForm())
{
@Html.DisplayFor(x => x.Items)
<button type="submit">OK</button>
}
</div>
并在编辑器模板(〜/查看/共享/ EditorTemplates / ItemViewModel.cshtml
)
@model ItemViewModel
<p>
@Html.LabelFor(model => model.Selected, Model.Name)
@Html.EditorFor(model => model.Selected)
</p>
现在预期它会工作。
这篇关于LabelFor环路不工作(用于/的foreach /模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!