简体   繁体   English

嵌套for循环 - RadioButtonFor not working

[英]Nested for loops - RadioButtonFor not working

I have nested foreach loops in my view, and am having problems with the radio buttons. 我在视图中嵌套了foreach循环,并且我遇到了单选按钮的问题。

@foreach (var header in Model.surveyHeaders)
{
    <h1>@header.Name</h1>

foreach (var subHeader in Model.surveySubHeaders.Where(x => x.SurveryHeaderID == header.ID))
{
<h2>@subHeader.Name</h2>

    foreach (var question in Model.surveyQuestions.Where(x => x.SurveySubHeaderID == subHeader.ID))
    {
        @Html.RadioButtonFor(x => x.surveyResults.Where(y => y.SurveyQuestionID == question.ID).First().Value, 1);
        @Html.RadioButtonFor(x => x.surveyResults.Where(y => y.SurveyQuestionID == question.ID).First().Value, 2);
        @Html.RadioButtonFor(x => x.surveyResults.Where(y => y.SurveyQuestionID == question.ID).First().Value, 3);            
        @question.Question
    }
}

} }

The radio button name is always 'Value', so the radio buttons are not grouped. 单选按钮名称始终为“值”,因此单选按钮不会分组。 What can I do to achieve the grouping that is desired? 我该怎么做才能实现所需的分组?

Ah collections in MVC, the joy! 在MVC的啊收藏,快乐! In order to make sure all fields are named accordingly in order to be correctly model bound on post, you need to use for loops, that will set the indexes correctly. 为了确保所有字段都相应命名以便在post上正确地进行模型绑定,您需要使用for循环,这将正确设置索引。

Before doing so, you're going to have to tweak your model structure to save your headaches. 在这样做之前,您将不得不调整模型结构以避免头痛。 You need to re-arrange your logic so that you have a hierarchical object model in which you can iterate more cleaner (this way we're getting away from logic in the view too!) 您需要重新排列逻辑,以便拥有一个分层对象模型,您可以在其中迭代更清晰(这样我们也可以远离视图中的逻辑!)

Your survey header class, can't you put a list of subheaders on it? 你的调查标题类,你不能把一个子标题列表放在上面吗? Then your subheader class, can you put the list of questions for that subheader? 然后你的子标题类,你能把那个子标题的问题列表? That way you can do: 这样你就可以做到:

@for (var i = 0; i < Model.SurveyHeaders.Count; i++)
{
    <h1>@Model.SurveyHeaders[i].Name</h1>

    for (var j = 0; j < Model.SurveyHeaders[i].SubHeaders.Count; j++)
    {
        <h2>@Model.SurveyHeaders[i].SubHeaders[j].Name</h2>

        for (var k = 0; k < Model.SurveyHeaders[i].SubHeaders[j].Questions.Count; k++)
        {
            @Html.RadioButtonFor(x => x.SurveyHeaders[i].SubHeaders[j].Questions[k].Value , 1);
            @Html.RadioButtonFor(x => x.SurveyHeaders[i].SubHeaders[j].Questions[k].Value , 2);
            @Html.RadioButtonFor(x => x.SurveyHeaders[i].SubHeaders[j].Questions[k].Value , 3);            
            @Model.SurveyHeaders[i].SubHeaders[j].Questions[k].Question
        }
    }
}

This assumes your new model structure is something like (with the following classes): 假设您的新模型结构类似于(使用以下类):

public class MyModel
{
    public List<SurveyHeader> SurveyHeaders { get; set; }
}

public class SurveyHeader
{
    public string Name { get; set; }
    public List<SubHeader> SubHeaders { get; set; }
}

public class SubHeader
{
    public string Name { get; set; }
    public List<Question> Questions { get; set; }
}

public class Question
{
    public int Value { get; set; }
    public string Question { get; set; }
}

Also one other tip, for future reference, you use the following LINQ in your code: 另外一个提示,为了将来参考,您在代码中使用以下LINQ:

x => x.surveyResults.Where(y => y.SurveyQuestionID == question.ID).First().Value

You can simplify it because First can actually take a lamba, like so (although you should use FirstOrDefault and null check it in future just for safety): 你可以简化它,因为First实际上可以采用lamba,就像这样(尽管你应该使用FirstOrDefault并在将来检查它只是为了安全):

x => x.surveyResults.First(y => y.SurveyQuestionID == question.ID).Value

您可以使用接收HtmlAttributes的RadioButtonFor扩展的重载并设置每个RadioButton的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM