2017-02-07 4 views
0

Ich habe mehrmals versucht, mein komplexes Objekt, das eine Liste enthält, korrekt in den POST zu bekommen. Alle Eigenschaften außer dem Listenobjekt enthalten Werte, wenn es veröffentlicht wird. Ich habe meine Annäherung an diese SO Q&A und diese SO Q&A basiert. In beiden Fällen besteht die Lösung darin, anstelle einer foreach-Schleife eine for-Schleife zu verwenden. Ich verwende die empfohlene for-Schleife, habe aber immer noch ein Problem. Ich bin mir nicht sicher, was sonst dazu führen könnte, dass die Liste beim Posten null ist. Vielen Dank im Voraus für Ihre Hilfe!MVC Razor POST - Listenobjekt ist immer Null

Ausblick:

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

Modell:

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}); 
     } 
+1

Ihr '[Bind]' - Attribut schließt "utilData" von der Bindung aus! (aber es ist Unsinn, alle diese Daten an die Ansicht und dann zurück zum Controller unverändert zu senden. Generieren Sie einfach Formularsteuerelemente für das, was Sie bearbeiten und erhalten Sie die anderen Daten in der POST-Methode, wenn Sie es erneut benötigen –

+0

Das hat den Trick. Danke ! Ich dachte, dass der POST das Modell spiegeln müsste, aber ich werde versuchen, die Menge der zurückgesendeten Daten zu reduzieren. – PvSyemya

+0

Ich hatte auch dieses Problem, bei dem ich '' Bind ''für die Überschreibung vergesse. – kimbaudi

Antwort

0

Per @Stephen Muecke ich einen Tippfehler hatte in der IndexMulti-Bindung:

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

utilData war ursprünglich Udata.