【问题标题】:MVC Model is null on postMVC 模型在帖子中为空
【发布时间】:2015-10-29 01:30:08
【问题描述】:

我的视图模型在 GET 上运行良好,但在发布后它为空,我不明白为什么。任何帮助将不胜感激。 Quiz-post 在这个阶段没有做太多的事情,我需要在完成方法之前访问发布的模型。 repository.GetAllQuestionsList() 返回 QuizViewModel 列表

视图模型

    public class QuizViewModel {
    public int Id { get; set; }
    public int QuestionId { get; set; } 
    public string QuestionText { get; set; } 
    public int QuestionNbr { get; set; } 
    public int CurrentQuestion { get; set; } 
    public int TotalNbrOfQuestions { get; set; } 
    public List<Answer> Answers { get; set; } 
}

控制器

    public ActionResult Quiz() {
        var getAllQuestions = repository.GetAllQuestionsList();
        var model = getAllQuestions.Where(c => c.QuestionNbr == 1);
        Session["CurrentQ"] = 1;
        return View(model);
    }

    [HttpPost]
    public ActionResult Quiz(IEnumerable<Models.QuizViewModel> model) {

        int question = Convert.ToInt32(Session["CurrentQ"]);
        string str = Request.Params["btnSubmit"];
        if (str == "Next Question") {
             question++;
            Session["CurrentQ"] = question;
        }
        if (str == "Prev Question") {
             question--;
            Session["CurrentQ"] = question;
        }

       return View(model);
    }

查看

@model IEnumerable<Quiz.Models.QuizViewModel>

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Quiz</title>
</head>

<body>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm()) {
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.QuestionText)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.QuestionNbr)
        </th>
        <th>
            answer
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.QuestionText)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.QuestionNbr)
            </td>
            <td>
                @foreach (var ML in item.Answers) {
                    @Html.RadioButtonFor(model => item.Answers, ML.Id)
                    @Html.Label(ML.AnswerText)
                    <br/>
                }
            </td>
        </tr>
    }

</table>
<input type="submit" id="Submit1" name="btnSubmit" value="Prev Question"/>
<input type="submit" id="Submit2" name="btnSubmit" value="Next Question"/>
}
</body>

</html>

【问题讨论】:

  • 您的视图代码没有意义。如果您的模型是IEnumerable,那么model.QuestionText 不应该工作(根本!)

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

我认为您的代码永远不会工作,因为名称绑定没有正确生成,@Html.DisplayNameFor(model =&gt; model.QuestionText)@Html.DisplayNameFor(model =&gt; model.QuestionNbr) 将抛出异常,因为 Model 是 IEnumerable 类型。 HttpPost 绑定仅在名称为输入控件时才有效,该控件在发布到服务器时在生成的 html 中适当生成。我已更正您的代码。请参考以下内容

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Quiz</title>
</head>

<body>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm()) {
<table class="table">

       <tr>
          <th>
            @Html.DisplayNameFor(model => model[0].QuestionText)
          </th>
          <th>
            @Html.DisplayNameFor(model => model[0].QuestionNbr)
          </th>
          <th>
             answer
          </th>
           <th></th>
        </tr>
    @for (int index = 0; index > Model.Count; index++) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => modelItem[index].QuestionText)
            </td>
            <td>
                @Html.DisplayFor(modelItem => modelItem[index].QuestionNbr)
            </td>
            <td>
                @for (int jIndex = 0; jIndex < Model[index].Answers; jIndex++ ) {
                    @Html.RadioButtonFor(model => modelItem[index].Answers[jIndex].Answer, modelItem[index].Answers[jIndex].Id)
                    @Html.Label(modelItem[index].Answers[jIndex].AnswerText)
                    <br/>
                }
            </td>
        </tr>
    }

</table>
<input type="submit" id="Submit1" name="btnSubmit" value="Prev Question"/>
<input type="submit" id="Submit2" name="btnSubmit" value="Next Question"/>
}
</body>

</html>

【讨论】:

    【解决方案2】:

    忽略@Html.DisplayNameFor(model =&gt; model.QuestionText)(正如我所说的没有意义)。您只需要更改您的迭代器,以便它知道该项目在数组中的位置,即

    @for (int i = 0; i < Model.Count; i++) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => modelItem[i].QuestionText)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => modelItem[i].QuestionNbr)
                </td>
           ..... etc
    }
    

    foreach 在 MVC 视图中不能很好地工作,因为 HTML 帮助程序不知道它在数组中的位置,并且名称没有正确构建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多