0
我是MVC的新手,並花了幾個小時試圖找到解決這個問題。我正在嘗試爲一些出租物業設置發票系統。財產管理者提供一個包含當前水讀數的excel文件。我必須扣除上個月的水量數據以瞭解每月的使用情況。我所做的觀點是這樣做的,並同時顯示所有單位。我遇到的問題是將上傳的文件和模型數據(上個月的讀數)一起傳遞給控制器。該文件顯示,但沒有其他數據。MVC更新視圖與上傳文件
在視圖中我有兩個提交按鈕。一個用於上傳要整合到模型數據中的文件,另一個用於基於之前和當前(上載的)數據創建新記錄。以下是相關的模型,視圖和控制器。
最終,賬單管理員會看到上個月的數據,上傳新數據,檢查並驗證沒有錯誤,然後提交新發票的數據。
如果有更好的方法來完成那麼我在這裏嘗試請讓我知道。這看起來好像用所有的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)
@* 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) |
<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> |
<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);
}
您好,我認爲這將是最好不要使用Excel的全部,你能不能創造一個經理可以將數據輸入網站上的形式? – GreatJobBob
不幸的是,目前這不是一個選項。 – PvSyemya