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

时间:2017-02-07 02:18:03

标签: c# asp.net-mvc razor

我已多次尝试获取包含正确POST列表的复杂对象。除列表对象外的所有属性在发布时都包含值。我的方法基于SO Q&ASO Q&A。但是,在这两种情况下,解决方案是使用for循环而不是foreach循环。我正在使用推荐的for循环,但仍然有问题。我不确定在发布时还有什么可能导致列表为空。在此先感谢您的帮助!

查看:

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

型号:

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

控制器:

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

1 个答案:

答案 0 :(得分:0)

Per @Stephen Muecke我在IndexMulti Binding中输入了一个拼写错误:

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

utilData最初是udata。