MVC Razor View失去了for循环中的可变范围

时间:2015-12-22 21:56:52

标签: c# asp.net-mvc razor

我有一个剃刀视图,一旦在forloop中丢失变量范围。它工作正常,直到我添加表标签。因此,我对ViewModel不同部分的所有调用都不再有效。如果还有其他有助于了解的信息,请告诉我。

@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>
}

2 个答案:

答案 0 :(得分:0)

我并不是100%确定你的问题究竟是什么,这个问题有点令人困惑,但你做的事情就是这样的地方:

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

应该用

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

即。使用m变量在lambda表达式中表示模型,而不是引用Model属性。

另外,在这一行:

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

您需要在@关键字前添加for

答案 1 :(得分:0)

尝试在该Razor块中使用foreach

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

更新:如果您的模型在发送时未映射到正确的ViewModel,则可能需要查看EditorForEditorForModel HTML帮助程序

相关问题