【发布时间】:2017-06-21 10:29:50
【问题描述】:
我是 MVC 的新手,我花了几个小时试图找到解决此问题的方法。我正在尝试为一些出租物业建立一个发票系统。物业管理器提供了一个包含当前水读数的 excel 文件。我必须减去上个月的水读数才能找出每月的使用量。我所做的视图做到了这一点,并同时显示了所有的单位。我遇到的问题是将上传的文件与模型数据(上个月的读数)一起传递给控制器。该文件显示,但没有其他数据。
在视图中,我有两个提交按钮。一个是上传要集成到模型数据中的文件,另一个是根据先前和当前(上传的)数据创建新记录。下面是相关的模型、视图和控制器。
帐单经理最终会看到上个月的数据,上传新数据,检查并确认没有错误,然后提交新发票的数据。
如果有更好的方法来实现这一点,那么我在这里尝试的方法请告诉我。这似乎使用所有 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,你不能在网站上创建一个表格,经理可以输入数据吗?
-
很遗憾,目前这不是一个选项。
标签: c# asp.net-mvc linq asp.net-mvc-4 upload