2017-02-03 7 views
0

私はMVCを初めて使い、この問題の解決策を探すのに数時間を費やしました。私はいくつかの賃貸物件の請求システムを設定しようとしています。プロパティマネージャーは、現在の水の読みを含むExcelファイルを提供します。私は先月からの水の測定値を引いて、毎月の使用量を調べる必要があります。私が作ったビューはこれを行い、すべてのユニットを同時に表示します。私が問題を抱えているのは、アップロードされたファイルをモデルデータ(過去数ヶ月の読み値)と共にコントローラに渡すことです。ファイルは表示されますが、他のデータは表示されません。アップロードされたファイルの更新ビュー

ビューには2つの送信ボタンがあります。 1つはモデルデータに統合するファイルをアップロードし、もう1つは前回と現在の(アップロードした)データに基づいて新しいレコードを作成するためのものです。関連するモデル、ビュー、コントローラは以下のとおりです。

最後に、請求担当者は先月のデータを確認し、新しいデータをアップロードし、エラーがないかどうかを確認して確認し、新しい請求書のデータを送信します。

それを達成するためのより良い方法があれば、ここで私が試みていることは私に教えてください。これは、すべてのlinqクエリでモデルデータを再作成する方が簡単なように思えました。あなたの助けを前もってありがとう!

モデル:

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

ビュー:

@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"}) 
 
     @*<input type="file" class="btn btn-info" name="postedFile"/>*@ 
 
    </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> 
 
    </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) 
 

 
         </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; 
 
                 @* TODO: fix date format *@ 
 
                 @Html.DisplayFor(model => model.bPrevDate) 
 
                </th> 
 
                <th>@Html.DisplayNameFor(model => model.utilData.First().wDifference)</th> 
 
                <th>Actions</th> 
 

 
               </tr> 
 
              </thead> 
 
              <!-- /table body--> 
 
              <tbody> 
 

 
               @foreach (var item in Model.utilData) 
 
               { 
 

 

 
                <tr role="row"> 
 
                 <td> 
 

 
                  @Html.DisplayFor(modelItem => item.ResNumber, null, "residence_" + item.rID) 
 
                  @Html.HiddenFor(model => item.GrnLower, new { id = "grnLower_" + item.rID }) 
 
                  @Html.HiddenFor(model => item.GrnUpper, new { id = "grnUpper_" + item.rID }) 
 
                  @Html.HiddenFor(model => item.YelLower, new { id = "yelLower_" + item.rID }) 
 
                  @Html.HiddenFor(model => item.YelUpper, new { id = "yelUpper_" + item.rID }) 
 
                 </td> 
 
                 <td> 
 
                  @Html.EditorFor(model => item.WaterReading, null, "waterReading_" + item.rID) 
 
                  
 
                 </td> 
 
                 <td> 
 
                  <span id="@string.Format("prevWater_{0}",item.rID)"> 
 
                   @Html.DisplayFor(model => item.prevWaterReading, null, "prevWater_" + item.rID) 
 
                  </span> 
 
                  @Html.HiddenFor(model => item.prevWaterReading, new { id = "hprevWater_" + item.rID }) 
 
                 </td> 
 
                 <td> 
 
                  <span id="@string.Format("hdifference_{0}",item.rID)"> 
 
                   @Html.DisplayFor(model => item.wDifference) 
 
                  </span> 
 
                  @Html.HiddenFor(model => item.prevWaterReading, new { id = "hdifference_" + item.rID }) 
 
                 </td> 
 
                 <td> 
 

 
                  @Html.ActionLink("View History", "ExportDataIndex", "ExportData", new { rID = item.rID, bId = Model.bID }, null) &nbsp;| &nbsp; 
 
                  <a href="@Url.Action("ExportToExcel", "ExportData", new { rID = item.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 = item.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-2 col-md-10"> 
 
        <input type="submit" value="Create" class="btn btn-default" /> 
 
       </div> 
 
      </div> 
 
    }

コントローラー:

// GET: ImportWater 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Upload([Bind(Include = "bEntryDate,bPrevDate,bID,utilData,UploadFile")]UtilityData uData) //<----The file gets uploaded but none of the Model data from the view. 
    { 
     HttpPostedFileBase postedFile = uData.UploadFile; 

      if (postedFile != null && postedFile.ContentLength > 0) 
      { 
      string fileName = postedFile.FileName; 
      string fileContentType = postedFile.ContentType; 
      byte[] fileBytes = new byte[postedFile.ContentLength]; 
      var data = postedFile.InputStream.Read(fileBytes, 0, Convert.ToInt32(postedFile.ContentLength)); 

      using (var package = new ExcelPackage(postedFile.InputStream)) 
      { 
       //Todo: read file and insert data 

      } 
      ViewBag.Message = "File uploaded successfully."; 
     } 

     return View(uData); 
    } 
+0

こんにちは、私はあなたが管理者がデータを入力することができ、サイト上のフォームを作成することはできません、すべてでExcelを使用しない方が良いだろうと思いますか? – GreatJobBob

+0

残念ながら、これは現在のところオプションではありません。 – PvSyemya

答えて

0

私が今何ISSUを理解しますeだった。私はPOSTがどのように働いたかを完全に理解していませんでした私は、フォームが常に完全なモデルオブジェクトを返すと思っていましたが、そうではありません。投稿したいモデルデータを取り込むために隠しアイテムを作成しました。

@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"}) 
 
     @*<input type="file" class="btn btn-info" name="postedFile"/>*@ 
 
    </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> 
 
}

関連する問題