这是一个MVC 4应用程序。代码如下:
<div id="example">
@if (Model.Questionnaires != null && Model.Questionnaires.Count > 0)
{
foreach (var item in Model.Questionnaires)
{
@Html.Label(@item.Title)
<div class="demo-section">
@foreach (var question in item.Questions)
{
<label id="@question.QuestionId.ToString()">@question.Text</label> <br />
@Html.Label(@question.Text ) <br />
}
</div>
<br />
}
}
else
{
<p>No matching lists where found.</p>
}
</div>
&#13;
结果输出如下所示:
<div id="example">
<label for="Pharmacy_Technician">Pharmacy Technician</label>
<div class="demo-section">
<label id="e4a2a2c9-2647-674e-b9fa-ff0000785e25">At least 18 years of age</label>
<br />
<label for="At_least_18_years_of_age">At least 18 years of age</label>
<br />
<label id="16a3a2c9-2647-674e-b9fa-ff0000785e25">Have a high school diploma or GED</label>
<br />
<label for="Have_a_high_school_diploma_or_GED">Have a high school diploma or GED</label>
<br />
<label id="24a3a2c9-2647-674e-b9fa-ff0000785e25">Have completed a training program or have a minimum 12 months of pharmacy related experience within the last 36 months</label>
<br />
<label for="Have_completed_a_training_program_or_have_a_minimum_12_months_of_pharmacy_related_experience_within_the_last_36_months">Have completed a training program or have a minimum 12 months of pharmacy related experience within the last 36 months</label>
<br />
</div>
<br />
<label for="Phlebotomy_Technician">Phlebotomy Technician</label>
<div class="demo-section">
<label id="afa5a2c9-2647-674e-b9fa-ff0000785e25">* Have successfully completed a training program or one year of work experience within the field. There is no specific time frame a candidate must test within once the training or work experience eligibility requirement has been met.</label>
<br />
<label for=""></label>
<br />
<label id="68a5a2c9-2647-674e-b9fa-ff0000785e25">* Posess a High School Diploma, or the equivalent</label>
<br />
<label for="">* Posess a High School Diploma, or the equivalent</label>
<br />
</div>
<br />
</div>
</div>
&#13;
正如您所看到的,助手正在创建一个空白标签。我不明白为什么Helper会杀死标签文本的价值。
我不明白的另一件事是为什么在第一组问题中填充为什么第二组是空的。
答案 0 :(得分:1)
LabelFor
帮助程序更合适,但是,由于它使用反射来创建命名,foreach
循环不提供索引上下文(因此它不知道如何正确命名它们)。
尝试使用for
循环代替LabelFor
:
for (var i = 0; i < Model.Questionnaires.Count; i++)
{
@Html.LabelFor(Model.Questionnaires[i])
<div class="demo-section">
@foreach (var q = 0; q < item.Questions.Count; q++)
{
<label id="@question.QuestionId.ToString()">@question.Text</label> <br />
@Html.LabelFor(@Model.Questionnaires[i].Questions[q]) <br />
}
</div>
<br />
}
为任何拼写错误道歉,因为这是直接输入内存的答案,但你明白了。
答案 1 :(得分:0)
它有一个星号(*),尝试用__替换星号并再次运行