我有以下代码:
foreach (var questionGroups in Model.ClientQuestionGroups)
{
for (var j = 0; j < questionGroups.Questions.Count; j++)
{
<div class="test">
@Html.HiddenFor(m => m.QuestionAnswers[j].FormAnswerId)
@Html.HiddenFor(m => m.QuestionAnswers[j].QuestionId)
@foreach (var alternative in questionGroups.Questions[j].QuestionAlternatives)
{
if (alternative.NestedQuestion != null)
{
nestedQuestions.Add(alternative.NestedQuestion);
}
}
</div>
}
if (nestedQuestions.Count > 0)
{
for (var i = 0; i < nestedQuestions.Count; i++)
{
<div class="nestedQuestion">
@Html.HiddenFor(m => m.QuestionAnswers[i].FormAnswerId)
@Html.HiddenFor(m => m.QuestionAnswers[i].QuestionId)
@foreach (var alt in nestedQuestions[i].QuestionAlternatives)
{
@Html.DisplayFor(m => alt.AlternativeText)
@Html.RadioButtonFor(m => m.QuestionAnswers[i].AlternativeId, alt.Id, new { @class = "questionAlternative" });
}
</div>
}
}
}
这会生成以下html:
<div class="test">
<input id="QuestionAnswers_0__FormAnswerId" type="hidden" value="00000000-0000-0000-0000-000000000000" name="QuestionAnswers[0].FormAnswerId" data-val-required="Fältet FormAnswerId krävs." data-val="true">
<input id="QuestionAnswers_0__QuestionId" type="hidden" value="427d0a77-b7b7-48b0-956a-28794145e0da" name="QuestionAnswers[0].QuestionId" data-val-required="Fältet QuestionId krävs." data-val="true">
Yes
<input id="QuestionAnswers_0__AlternativeId" class="questionAlternative" type="radio" value="aabf2548-abca-4cb5-89d8-759f2d71be01" nestedquestion="895015c0-2bf0-4c5a-95c9-8d4b29671bf7" name="QuestionAnswers[0].AlternativeId" data-val-required="Fältet AlternativeId krävs." data-val="true">
No
<input id="QuestionAnswers_0__AlternativeId" class="questionAlternative" type="radio" value="91b220dc-3ef3-45a9-8369-dc1e4e2530a8" nestedquestion="" name="QuestionAnswers[0].AlternativeId">
</div>
<div class="nestedQuestion questionDiv">
<p>Testar2</p>
<p>Testar2</p>
<input id="QuestionAnswers_0__FormAnswerId" type="hidden" value="00000000-0000-0000-0000-000000000000" name="QuestionAnswers[0].FormAnswerId">
<input id="QuestionAnswers_0__QuestionId" type="hidden" value="427d0a77-b7b7-48b0-956a-28794145e0da" name="QuestionAnswers[0].QuestionId">
Yes
<input id="QuestionAnswers_0__AlternativeId" class="questionAlternative" type="radio" value="aabf2548-abca-4cb5-89d8-759f2d71be01" name="QuestionAnswers[0].AlternativeId">
No
<input id="QuestionAnswers_0__AlternativeId" class="questionAlternative" type="radio" value="91b220dc-3ef3-45a9-8369-dc1e4e2530a8" name="QuestionAnswers[0].AlternativeId">
</div>
如您所见,ID-attribute和Name-attribute具有相同的值/名称。我不希望这样。我希望第一个div为0
,嵌套的问题div为1
,依此类推。我知道这是因为它处于相同的循环中。
我无法弄清楚如何解决这个命名架构。谁能把我推向正确的方向?
答案 0 :(得分:1)
for(var i = 0; i&lt; nestedQuestions.Count; i ++)
将第二个for循环更改为以下内容:
for(var i = 1; i&lt; nestedQuestions.Count; i ++)
但这会很糟糕,因为当你有多个问题和多个嵌套问题时,他们会混淆不清。
也许这样的事情会更好:
if (nestedQuestions.Count > 0)
{
for (var i = 0; i < nestedQuestions.Count; i++)
{
<div class="nestedQuestion">
@Html.Hidden("NestedQuestions", Model.QuestionAnswers[i].FormAnswerId)
@Html.Hidden("NestedQuestions", Model.QuestionAnswers[i].QuestionId)
@foreach (var alt in nestedQuestions[i].QuestionAlternatives)
{
@Html.Display("NestedQuestion", alt.AlternativeText)
@Html.RadioButton("NestedQuestion", m.QuestionAnswers[i].AlternativeId, alt.Id, new { @class = "questionAlternative" });
}
</div>
}
}
这将生成你想要的东西,但在你的Post动作中你应该有一些属性来捕获这些嵌套对象。当你使名字匹配时,defaultModelBinder将绑定它。
问候,
斯文
答案 1 :(得分:0)
你在哪里建造它?我想这是你的观点?在我看来,这不是最佳实践,因为你在那里放了太多的逻辑。是否无法在业务层/ mvc(控制器)中计算此值。所有if语句都太多了。在MVC中你应该尽可能避免视图中的逻辑。当我修复那个部分你可以在没有for循环的foreach中做foreach。使管理变得更加简单。
首先,for循环是否必要?
问候,
秒。
答案 2 :(得分:0)
没有想到的例子..但是这可以帮助..
@{ int testCounter = 0; int nestedQuestionsCounter = 0; }
@foreach (var questionGroups in Model.ClientQuestionGroups)
{
for (var j = 0; j < questionGroups.Questions.Count; j++)
{
<div class="@(testCounter)test">
@Html.HiddenFor(m => m.QuestionAnswers[j].FormAnswerId)
@Html.HiddenFor(m => m.QuestionAnswers[j].QuestionId)
@foreach (var alternative in questionGroups.Questions[j].QuestionAlternatives)
{
if (alternative.NestedQuestion != null)
{
nestedQuestions.Add(alternative.NestedQuestion);
}
}
</div>
}
if (nestedQuestions.Count > 0)
{
nestedQuestionsCounter = 1;
for (var i = 0; i < nestedQuestions.Count; i++)
{
<div class="@(nestedQuestionsCounter)nestedQuestion">
@Html.HiddenFor(m => m.QuestionAnswers[i].FormAnswerId)
@Html.HiddenFor(m => m.QuestionAnswers[i].QuestionId)
@foreach (var alt in nestedQuestions[i].QuestionAlternatives)
{
@Html.DisplayFor(m => alt.AlternativeText)
@Html.RadioButtonFor(m => m.QuestionAnswers[i].AlternativeId, alt.Id, new { @class = "questionAlternative" });
}
</div>
nestedQuestionsCounter++;
}
}
testCounter++;
}