简体   繁体   English

MVC Razor POST-列表对象始终为null

[英]MVC Razor POST - list object is always null

I've tried several times to get my complex object which contains a list to POST correctly. 我已经尝试了好几次来获取我的复杂对象,其中包含要正确发布的列表。 All the properties except the list object contain values when it posts. 除列表对象之外的所有属性在发布时均包含值。 I've based my approach on this SO Q&A and this SO Q&A . 我的方法基于此SO Q&ASO Q&A However, in both case the solution is to use a for loop instead of a foreach loop. 但是,在两种情况下,解决方案都是使用for循环而不是foreach循环。 I'm using the recommended for loop but still have\\ing an issue. 我正在使用推荐的for循环,但仍然有问题。 I'm not sure what else could be causing the list to be null when posting. 我不确定发布时该列表为null的其他原因。 Thanks in advance for your help! 在此先感谢您的帮助!

View: 视图:

 @model PropertiesAdminSite.Models.UtilityData @{ ViewBag.Title = "CreateNewCycle"; } <h2>New Residence Utilities</h2> @using (Html.BeginForm("Upload", "ImportWater", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() <div class="control-group"> @Html.TextBoxFor(m => m.UploadFile, new { type = "file"}) </div> <div class="control-group"> <input type="submit" class="btn btn-info" value="Upload" /> </div> <div class="col-lg-12 visible-lg"> <br> <span style="color:green">@ViewBag.Message</span> @Html.HiddenFor(model => model.bID) @Html.HiddenFor(model => model.bEntryDate) @Html.HiddenFor(model => model.bPrevDate) @for (int i = 0; i < Model.utilData.Count(); i++) { @Html.HiddenFor(model => model.utilData[i].ResNumber) @Html.HiddenFor(model => model.utilData[i].GrnLower) @Html.HiddenFor(model => model.utilData[i].GrnUpper) @Html.HiddenFor(model => model.utilData[i].prevWaterReading) @Html.HiddenFor(model => model.utilData[i].rID) @Html.HiddenFor(model => model.utilData[i].WaterReading) @Html.HiddenFor(model => model.utilData[i].wDifference) @Html.HiddenFor(model => model.utilData[i].YelLower) @Html.HiddenFor(model => model.utilData[i].YelUpper) } </div> } @using (Html.BeginForm("IndexMulti", "Utilities", FormMethod.Post)) { @Html.AntiForgeryToken() <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="row"> <div class="col-lg-12"> <div class="panel panel-default"> <div class="panel-heading"> @Html.LabelFor(model => model.bEntryDate, htmlAttributes: new { @class = "control-label col-md-1" }) @Html.DisplayFor(model => model.bEntryDate) @Html.HiddenFor(model => model.bID) @Html.HiddenFor(model => model.bEntryDate) @Html.HiddenFor(model => model.bPrevDate) </div> <!-- /.panel-heading --> <div class="panel-body"> <div class="dataTable_wrapper"> <!--div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">--> <div class="row"> <div class="col-sm-12"> <table class="table table-striped table-bordered table-hover dataTable no-footer" id="dataTables-Bills" role="grid" aria-describedby="dataTables-example_info"> <!-- /table headers--> <thead> <tr role="row"> <th>@Html.DisplayNameFor(model => model.utilData.First().ResNumber)</th> <th>@Html.DisplayNameFor(model => model.utilData.First().WaterReading)</th> <th> @Html.DisplayNameFor(model => model.utilData.First().prevWaterReading) &nbsp; @Html.DisplayFor(model => model.bPrevDate) </th> <th>@Html.DisplayNameFor(model => model.utilData.First().wDifference)</th> <th>Actions</th> </tr> </thead> <!-- /table body--> <tbody> @for (int i = 0; i < Model.utilData.Count(); i++) { <tr role="row"> <td> @Html.DisplayFor(modelItem => modelItem.utilData[i].ResNumber) @Html.HiddenFor(model => model.utilData[i].GrnLower) @Html.HiddenFor(model => model.utilData[i].GrnUpper) @Html.HiddenFor(model => model.utilData[i].YelLower) @Html.HiddenFor(model => model.utilData[i].YelUpper) </td> <td> @Html.EditorFor(model => model.utilData[i].WaterReading) </td> <td> <span id="@string.Format("prevWater_{0}",Model.utilData[i].rID)"> @Html.DisplayFor(model => model.utilData[i].prevWaterReading) </span> @Html.HiddenFor(model => model.utilData[i].prevWaterReading) </td> <td> <span id="@string.Format("hdifference_{0}",Model.utilData[i].rID)"> @Html.DisplayFor(model => model.utilData[i].wDifference) </span> @Html.HiddenFor(model => model.utilData[i].prevWaterReading) </td> <td> @Html.ActionLink("View History", "ExportDataIndex", "ExportData", new { rID = Model.utilData[i].rID, bId = Model.bID }, null) &nbsp;| &nbsp; <a href="@Url.Action("ExportToExcel", "ExportData", new { rID = Model.utilData[i].rID, bId = Model.bID })" class="btn btn-success"> <i class="fa fa-file-excel-o" aria-hidden="true" title="Export to Excel"></i> </a>&nbsp;| &nbsp; <a href="@Url.Action("ChartData", "Utilities", new { rID = Model.utilData[i].rID, bId = Model.bID })" class="btn btn-info"> <i class="fa fa-bar-chart" aria-hidden="true" title="Water Usage History"></i> </a> </td> </tr> } </tbody> </table> </div> </div> </div> </div> </div> </div> </div> <div class="form-group"> <div class="col-md-offset-0 col-md-8"> <input type="submit" value="Submit Water Readings and Create Invoices" class="btn btn-primary btn-lg btn-block" /> </div> </div> } 

Model: 模型:

public partial class UtilityData
{
    public DateTime bEntryDate { get; set; }
    public string bPrevDate { get; set; }
    public int bID { get; set; }
    //public int residenceCount { get; set; }
    public List<UtilEntry> utilData { get; set; }
    public HttpPostedFileBase UploadFile { get; set; }


}

public partial class UtilEntry
{
    public int rID { get; set; }
    public long? WaterReading { get; set; }
    public int ResNumber { get; set; }
    public long? prevWaterReading { get; set; }
    public decimal wDifference { get; set; }
    public int GrnUpper { get; set; }
    public int GrnLower { get; set; }
    public int YelUpper { get; set; }
    public int YelLower { get; set; }
}

Controller: 控制器:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult IndexMulti([Bind(Include = "bEntryDate, bPrevDate,bID,uData")] UtilityData uData)
    {
        if (ModelState.IsValid)
        {
            Omitted for clarity
         }

            return RedirectToAction("Create", "TenantAccount", new { id = uData.bID});
        }

Per @Stephen Muecke I had a typo in the IndexMulti Binding: 根据@Stephen Muecke,我在IndexMulti绑定中有一个错字:

 public ActionResult IndexMulti([Bind(Include = "bEntryDate, bPrevDate,bID,utilData")] UtilityData uData)

utilData was udata originally. utilData最初是udata。

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

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