简体   繁体   English

MVC 3 Webapp-模型活页夹

[英]MVC 3 Webapp - Model Binder

I am creating my first MVC app and one of the pages need to list a series of questions from a table in a database. 我正在创建我的第一个MVC应用程序,其中一个页面需要列出数据库表中的一系列问题。 I have set-up the Model: 我已经设置了模型:

public class Audit
{
    public DateTime InspectionDate { get; set; }
    public string Engineer { get; set; }
    public List<AuditGroup> AuditGroups { get; set; }
}

public class AuditGroup
{
    public string GroupName { get; set; }
    public List<AuditQuestion> AuditQuestions { get; set; }
}

public class AuditQuestion
{
    public string Group { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
    public string Comments { get; set; }
}

The models contains Lists, I created a page called Create and populated the model with the groups and questions in the groups and these displayed on the page fine. 模型包含列表,我创建了一个名为Create的页面,并在模型中填充了组和组中的问题,并在页面上很好地显示了这些问题。 When I answer the questions (fill in a textbox) and press the submit button it calls the controller: 当我回答问题(在文本框中填写)并按提交按钮时,它将调用控制器:

    [HttpPost]
    public ActionResult Create(Audit newAudit)
    {
        try
        {
            // TODO: Add insert logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

The newAudit has data in the Engineer and Inspection Date but the binder is not picking up the Lists on the page. newAudit在“工程师和检验日期”中有数据,但活页夹未在页面上选取列表。 This is a cut down version I have trying to work it out: 这是我尝试解决的简化版本:

@{
    ViewBag.Title = "Audit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model test.Models.Audit
<h2>
    Audit</h2>
@using (Html.BeginForm())
{
<fieldset>

    @foreach (var AuditGroups in Model.AuditGroups)
    { 
     @Html.EditorFor(x=> AuditGroups.GroupName)

        }
</fieldset>
<p>
    <input type="submit" value="Create" />
</p>

} }

So here I am going through the list and putting them on the page but on submit the list is null. 所以在这里,我浏览列表并将其放在页面上,但是在提交时列表为空。 Does anyone know where I am going wrong? 有人知道我要去哪里错吗?

In summary what I am doing is sending a list of question to page, user to fill in and submit the answers but im not getting the answers for any of the Lists. 总而言之,我正在做的是向页面发送问题列表,用户填写并提交答案,但我无法获得任何列表的答案。

please have a look at my answer to this question . 请看看我对这个问题的回答 it contains some blog posts that addressed this issue. 它包含一些解决此问题的博客文章。 Furthermore, read Yarx's comment on the answer in which he provided the link to a post which helped him solve the problem. 此外,请阅读Yarx对答案的评论,他在其中提供了指向有助于他解决问题的帖子的链接。

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

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