简体   繁体   English

MVC Razor View在for循环中丢失了变量范围

[英]MVC Razor View loses Variable scope inside for loop

I have a razor view that loses the variable scope once inside the forloop. 我有一个剃刀视图,一旦进入forloop,它就会失去变量作用域。 It works fine until I added the table tags. 在添加表标签之前,它工作正常。 So all my calls to the different parts of the ViewModel no longer work. 因此,我对ViewModel不同部分的所有调用都不再起作用。 Please let me know if there is anything else that would help to know. 如果还有其他需要帮助的地方,请告诉我。

@model Auditor_Evaluations_MVC.ViewModels.EvaluationViewModel

@using (Html.BeginForm())
{
    <div>
        <table class="table table-condensed" style="width:825px;"> 
            for(int i = 0; i < Model.QuestionGroup.Count; i++)
            {       
                var questionGroup = Model.QuestionGroup[i];
                @Html.HiddenFor(m => Model.QuestionGroup[i].GroupName)
                @Html.HiddenFor(m => Model.QuestionGroup[i].GroupId)
                <thead>
                    <tr>
                        <th class="text-left">@Html.Label(questionGroup.GroupName) </th>
                        <th class="text-center">Excellent</th>
                        <th class="text-center">Good</th>
                        <th class="text-center">Fair</th>
                        <th class="text-center">Poor</th>
                        <th class="text-center">N/A</th>
                    </tr>
                </thead>
                var questions = questionGroup.Questions;
                for(int j = 0; j < questions.Count; j++)
                {  
                    @Html.HiddenFor(m => Model.QuestionGroup[i].Questions[j].QuestionName)     
                    <tr>
                        <td class="text-left">@Html.Label(questions[j].QuestionName) </td>
                            var questionResponse = questions[j].QuestionResponses;
                            @for(int k = 0; k < questionResponse.Count; k++)
                            {
                                <td class="text-center" style="width:75px">
                                    @Html.RadioButtonFor(m => Model.QuestionGroup[i].Questions[j].SelectedAnswer,questionResponse[k].QuestionValue)

                                </td>
                            }

                    </tr>

                }   
                }             


            <tr>
                <td colspan="6">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="6"><strong>@Model.GeneralQuestion1Text</strong></td>
            </tr>
            <tr>
                <td colspan="6">@Html.TextAreaFor(m => m.GeneralQuestion1Value)</td>
            </tr>
            <tr>
                <td colspan="6"><strong>2. Additional Comments:</strong></td>
            </tr>
            <tr>
                <td colspan="6">@Html.TextAreaFor(m => m.GeneralQuestion2Value)</td>
            </tr>
            <tr>
                <td colspan="6"><strong>3. Would you like the appropriate audit manager to contact you regarding any or all of the information relayed through this questionnaire? If yes, please include your name and telephone number.</strong></td>
            </tr>
            <tr>
                <td colspan="6">@Html.TextAreaFor(m => m.GeneralQuestion3Value)</td>
            </tr>
            <tr>
                <td colspan="6"><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    </div>
}

I'm not 100% sure on exactly what your problem is, the question is a little confusiing, but all the places where you do something like this: 我不确定您的问题到底是什么100%,这个问题有点令人困惑,但是您在所有进行此类操作的地方都:

@Html.HiddenFor(m => Model.QuestionGroup[i].GroupName)

Should probably be replaced with 应该应该替换为

@Html.HiddenFor(m => m.QuestionGroup[i].GroupName)

ie use the m variable to represent your model in the lambda expression, rather than referencing the Model property. 即使用m变量在lambda表达式中表示您的模型,而不是引用Model属性。

Also, on this line: 另外,在此行上:

<table class="table table-condensed" style="width:825px;"> 
     for(int i = 0; i < Model.QuestionGroup.Count; i++)

You need to add an @ in front of the for keyword. 您需要在for关键字前面添加一个@

Try using a foreach in that Razor block 尝试在该Razor区块中使用foreach

foreach(var question in Model.QuestionGroup) {
  @Html.HiddenFor(m => m.question.GroupName)
  @Html.HiddenFor(m => m.question.GroupId)
...
etc.

update: you may need to look into the EditorFor and EditorForModel HTML helpers if your model is not mapping to the correct ViewModel when send it 更新:如果模型在发送时未映射到正确的ViewModel,则可能需要查看EditorForEditorForModel HTML帮助器

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

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