带上传文件的MVC Update视图

时间:2017-02-03 23:28:22

标签: c# asp.net-mvc linq asp.net-mvc-4 upload

我是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) &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);
    }

1 个答案:

答案 0 :(得分:0)

我现在明白这个问题是什么了;我没有完全理解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>
}