MVC模型在发布期间为空

时间:2014-09-08 10:25:12

标签: asp.net-mvc http-post

大家好我发帖时遇到问题,我的模型在http帖子上似乎是空的,这很奇怪,在get方法中没有问题,所有内容都应该填充但是在post模型中是null。请帮忙。

查看

  @model ComplianceCheck.Models.ViewModel.ArrivalModel

  @for (int itemindex = 0, id = itemindex + 1; itemindex < Model.questionnaires.Count; itemindex++)
    {

        <tr>
            <td>
                @id
            </td>
            <td> 
                @Html.DisplayFor(modelItem => Model.questionnaires[itemindex].Description)
            </td>
            <td>
                YES
                @Html.RadioButtonFor(modelItem => Model.questionnaires[itemindex].Answer, true, new { id = "rbYes", @checked = "checked" }) 
                 NO
                @Html.RadioButtonFor(modelItem => Model.questionnaires[itemindex].Answer, false, new { id = "rbNo" })

            </td>
             <td>
                  @Html.DisplayFor(modelItem => Model.questionnaires[itemindex].WigsID)
             </td>
            <td>  

                  @Html.TextBoxFor(modelItem => Model.questionnaires[itemindex].Remarks, new { @style = "width:250px;" })
            </td>
        </tr>

模型

 public class ArrivalModel
{

    public ArrivalSearchModel searchModel = new ArrivalSearchModel();
    public List<ArrivalDataModel> data = new List<ArrivalDataModel>();
    public ArrivalSingleDataModel singledata = new ArrivalSingleDataModel();
    public List<ArrivalQuestionnaires> questionnaires = new List<ArrivalQuestionnaires>();
}

  public class ArrivalQuestionnaires
{
    public int QuestionID { get; set; }

    public string WigsID { get; set; }

    public string Area { get; set; }

    public string Description { get; set; }

    public bool Answer { get; set; }

    public string Remarks { get; set; }

}

控制器:删除了一些代码以减少问题长度

//搞定工作..

 GET: /Questionnaires/ (Create)
      public ActionResult QuestionnairesList(string txtDate, string Flightno, string Checker)
        {
            ArrivalModel model = new ArrivalModel();

            List<ArrivalQuestionnaires> arrivalQuestionnaires = new List<ArrivalQuestionnaires>();

  foreach (Question questionnaires in ArrivalService.GetAllQuestionnaires("Arrival"))
                {
                    ArrivalQuestionnaires toAdd = new ArrivalQuestionnaires
                    {
                        QuestionID = questionnaires.QuestionID,
                        WigsID = questionnaires.Wig.WigsCode,
                        Area = questionnaires.Area,
                        Description = questionnaires.Description,
                    };
                    arrivalQuestionnaires.Add(toAdd);
                }

                model.questionnaires = arrivalQuestionnaires;
            }

            return View(model);
        }

}

这是问题,httppost会触发,但模型为空

[HttpPost]
        public ActionResult QuestionnairesList(ArrivalModel model)
        {}

3 个答案:

答案 0 :(得分:4)

您的模型没有getter / setter

public class ArrivalModel
{
  // Add constructor if you want to initialize properties
  public ArrivalModel()
  {
    searchModel = new ArrivalSearchModel();
    data = new List<ArrivalDataModel>();
    singledata = new ArrivalSingleDataModel();
    questionnaires = new List<ArrivalQuestionnaires>();
  }
  public ArrivalSearchModel searchModel { get; set; }
  public List<ArrivalDataModel> { get; set; }
  public ArrivalSingleDataModel singledata { get; set; }
  public List<ArrivalQuestionnaires> questionnaires { get; set; }
}

答案 1 :(得分:0)

问题是你的模型充满了集合,试着阅读这篇文章来绑定模型

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

答案 2 :(得分:0)

为此,create editorTemplate for ArrivalQuestionnaires然后绑定到ArrivalModel中的数据。

More information about editor Template