c# - MVC 模型在发布时为空

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

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

View 模型

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

Controller

    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>

最佳答案

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

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

关于c# - MVC 模型在发布时为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33396616/

相关文章:

asp.net-mvc - 网络农场中的 nHibernate 策略

asp.net-mvc-4 - 哪个machineKey配置更好?

c# - MVC4 WebSecurity.CurrentUserId 返回 -1 而 User.Identity.Name 仍然有效

c# - 从 C# 编码 URL,用 PHP 解密 URL(以某种方式添加额外字符)

C# Winforms 动态菜单条目

c# - ASP.NET 在捆绑中使用嵌入式资源

javascript - 如何检测并拒绝对 MVC Controller 的非 JavaScript 发布

asp.net-mvc-4 - 我可以按特定顺序将两个 bundle 合并在一起,例如将两个 bundle 连接起来并为其生成一个 bundle 吗?

c# - page_load 和 onLoad 的区别

c# - 使用 webjobs 调用另一个网站的 api