加载应用程序时,会显示包含项目列表的表格。
单击“保存”按钮,我想将项目列表写入excel。
当我点击保存按钮时,在新请求中发送给控制器,列表为空。我不想将表项列表写入数据库。
有人可以建议我如何处理这件事吗?
public IActionResult SaveReport(SalesParentViewModel salesParentViewModel)
{
if(salesParentViewModel.SalesDataModelItems != null)
{
var buffer = new StringBuilder();
buffer.AppendLine("#UserCode,SalesmanName,Date,ItemCode,ItemDescription,BrandCode,BrandName,ClientCode,Client,ClientBranchCode,Description, BranchSubChannel,TransactionAmount,Quantity");
salesParentViewModel.SalesDataModelItems.ToList().ForEach(item => buffer.AppendLine
(String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}", item.UserCode, item.SALESMANNAME, item.DATE, item.ItemCode, item.ITEMDESCRIPTION, item.BRANDCODE, item.BRANDNAME, item.ClientCode, item.Client, item.ClientBranchCode, item.Description, item.BRANCHSUBCHANNEL, item.TrxAmount, item.QTY
)));
System.IO.File.WriteAllText("c:\\temp\\file.csv", buffer.ToString());
}
return View();
}
查看如下:
@model MyStoreReports.ViewModels.SalesParentViewModel
@{
ViewBag.Title = "Sales Report";
}
@section scripts{
<script src="~/lib/jquery-validation-unobtrusive/dist/jquery.validate.unobtrusive.js"></script>
<link rel="stylesheet"
href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css">
<script type="text/javascript"
src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<script type="text/javascript"
src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
}
<div class="container-fluid">
<div class="row">
<div class="col-sm-9">
<h1>Sales Report</h1>
</div>
</div>
<div>
<asp:Button id="btnFilter" class="btn btn-sm btn-info" runat="server" Text="Click Me!">
<i class="fa fa-filter"></i>Filter
</asp:Button>
@using (Html.BeginForm("SaveReport", "App", FormMethod.Post))
{
@Html.HiddenFor(model => model.SalesDataModelItems);
<asp:Button id="btnSave" class="btn btn-sm btn-info" onclick="location.href='@Url.Action("SaveReport","App")'" runat="server" Text="Click Me!">
<i class="fa fa-save"></i>Save
</asp:Button>
}
@*<a href="#" class="btn btn-sm btn-info">
<i class="fa fa-save"></i>Save
</a>*@
<a href="#" class="btn btn-sm btn-info">
<i class="fa fa-print"></i>Print
</a>
</div>
<div class="row">
<form method="post">
<asp:Panel id="pnlFilter" runat="server" GroupingText="This is a sample group text" HorizontalAlign="Center">
<div class="col-sm-10" style="background-color:lavenderblush;">
<div class="row">
<div class="col-sm-2">
<label asp-for="SalesViewModelInstance.StartDate">StartDate</label>
<div class="form-group">
<input asp-for="SalesViewModelInstance.StartDate" type="date" class="form-control" />
<span asp-validation-for="SalesViewModelInstance.StartDate" class="text-muted"></span>
</div>
</div>
<div class="col-sm-2">
<label asp-for="SalesViewModelInstance.EndDate">EndDate</label>
<div class="form-group">
<input asp-for="SalesViewModelInstance.EndDate" type="date" class="form-control" />
<span asp-validation-for="SalesViewModelInstance.EndDate" class="text-muted"></span>
</div>
</div>
<div class="row">
<div class="col-sm-1">
<input type="submit" value="Submit" class="btn btn-success" />
</div>
<div class="col-sm-1">
<a asp-controller="App" asp-action="Index" class="btn btn-default">Cancel</a>
</div>
</div>
</div>
</asp:Panel>
</form>
</div>
<div class="form-group">
@if (Model.SalesDataModelItems != null)
{
<div class="container" style="background-color:lavender;" >
<div class="row">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>Report</h2>
</div>
<div class="VScrollTable">
<table id="myTable" class="table table-fixed table-responsive" align="left" cellspacing="0">
<thead>
<tr>
<th>UserCode</th>
<th>SalesmanName</th>
<th>Date</th>
<th>ItemCode</th>
<th>ItemDescription</th>
<th>BrandCode</th>
<th>BrandName</th>
<th>ClientCode</th>
<th>Client</th>
<th>ClientBranchCode</th>
<th>Description</th>
<th>BranchSubChannel</th>
<th>TransactionAmount</th>
<th>Quantity</th>
</tr>
</thead>
@foreach (var item in Model.SalesDataModelItems)
{
<tbody>
<tr>
<td>@item.UserCode</td>
<td>@item.SALESMANNAME</td>
<td>@item.DATE</td>
<td>@item.ItemCode</td>
<td>@item.ITEMDESCRIPTION</td>
<td>@item.BRANDCODE</td>
<td>@item.BRANDNAME</td>
<td>@item.ClientCode</td>
<td>@item.Client</td>
<td>@item.ClientBranchCode</td>
<td>@item.Description</td>
<td>@item.BRANCHSUBCHANNEL</td>
<td>@item.TrxAmount</td>
<td>@item.QTY</td>
</tr>
</tbody>
}
</table>
</div>
</div>
</div>
</div>
}
</div>
</div>
答案 0 :(得分:1)
如果模型中没有进行任何更改,为什么要将整个模型从视图传递到控制器? 只需传递一些像id这样的标识符,然后在控制器中再次获取数据并将其保存在Excel中。
如果必须将模型传递回控制器,则可以完成,但数据需要隐藏输入或可编辑输入控件(文本框,复选框)
您的观点应与以下内容类似:
@model MyStoreReports.ViewModels.SalesParentViewModel
@{
ViewBag.Title = "Sales Report";
}
@section scripts{
<script src="~/lib/jquery-validation-unobtrusive/dist/jquery.validate.unobtrusive.js"></script>
<link rel="stylesheet"
href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css">
<script type="text/javascript"
src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<script type="text/javascript"
src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
}
<div class="container-fluid">
<div class="row">
<div class="col-sm-9">
<h1>Sales Report</h1>
</div>
</div>
<div>
@using (Html.BeginForm("SaveReport", "App", FormMethod.Post))
{
<input id="brnSave" type="submit" class="btn btn-sm btn-info" value="Save" />
<div class="form-group">
@if (Model.SalesDataModelItems != null)
{
<div class="container" style="background-color:lavender;">
<div class="row">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>Report</h2>
</div>
<div class="VScrollTable">
<table id="myTable" class="table table-fixed table-responsive" align="left" cellspacing="0">
<thead>
<tr>
<th>UserCode</th>
<th>SalesmanName</th>
<th>Date</th>
<th>ItemCode</th>
<th>ItemDescription</th>
<th>BrandCode</th>
<th>BrandName</th>
<th>ClientCode</th>
<th>Client</th>
<th>ClientBranchCode</th>
<th>Description</th>
<th>BranchSubChannel</th>
<th>TransactionAmount</th>
<th>Quantity</th>
</tr>
</thead>
@for (int i = 0; i < Model.SalesDataModelItems.Count; i++)
{
<tbody>
<tr>
<td>@Model.SalesDataModelItems[i].UserCode</td>
<td>@Model.SalesDataModelItems[i].SalesManName</td>
<td>@Model.SalesDataModelItems[i].SaleDate</td>
<td>@Model.SalesDataModelItems[i].ItemCode</td>
<td>@Model.SalesDataModelItems[i].ItemDescription</td>
<td>@Model.SalesDataModelItems[i].BrandCode</td>
<td>@Model.SalesDataModelItems[i].BrandName</td>
<td>@Model.SalesDataModelItems[i].ClientCode</td>
<td>@Model.SalesDataModelItems[i].ClientName</td>
<td>@Model.SalesDataModelItems[i].ClientBranchCode</td>
<td>@Model.SalesDataModelItems[i].Description</td>
<td>@Model.SalesDataModelItems[i].BranchSubChannel</td>
<td>@Model.SalesDataModelItems[i].TrxAmount</td>
<td>@Model.SalesDataModelItems[i].Quantity</td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].UserCode) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].SalesManName) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].SaleDate) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].ItemCode) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].ItemDescription) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].BrandCode) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].BrandName) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].ClientCode) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].ClientName) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].ClientBranchCode) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].Description) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].BranchSubChannel) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].TrxAmount) </td>
<td>@Html.HiddenFor(model => model.SalesDataModelItems[i].Quantity) </td>
</tr>
</tbody>
}
</table>
</div>
</div>
</div>
</div>
}
</div>
}
</div>
</div>
和Tetsuya Yamamoto指出的一样,避免在asp.net MVC应用程序中混合asp web表单控件
编辑(玛丽亚评论后):
您可以添加ajax调用,以便在按下按钮时保存excel中的数据:
Controller Savereport方法:
public ActionResult SaveReport()
{
try
{
// Get data from DB and save it in excel
return Json("Success save report");
}
catch (Exception ex)
{
return Json("Save report failure!" + ex.Message);
}
}
查看:
@model MVCWebApplication.Controllers.SalesParentViewModel
@{
ViewBag.Title = "Sales Report";
}
@section scripts{
<link rel="stylesheet"
href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css">
<script type="text/javascript"
src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<script type="text/javascript"
src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
}
<div class="container-fluid">
<div class="row">
<div class="col-sm-9">
<h1>Sales Report</h1>
</div>
</div>
<div>
<input id="btnSave" type="button" class="btn btn-sm btn-info" data-url="@Url.Action("SaveReport","App")" value="Save" />
<div class="form-group">
@if (Model.SalesDataModelItems != null)
{
<div class="container" style="background-color:lavender;">
<div class="row">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>Report</h2>
</div>
<div class="VScrollTable">
<table id="myTable" class="table table-fixed table-responsive" align="left" cellspacing="0">
<thead>
<tr>
<th>UserCode</th>
<th>SalesmanName</th>
<th>Date</th>
<th>ItemCode</th>
<th>ItemDescription</th>
<th>BrandCode</th>
<th>BrandName</th>
<th>ClientCode</th>
<th>Client</th>
<th>ClientBranchCode</th>
<th>Description</th>
<th>BranchSubChannel</th>
<th>TransactionAmount</th>
<th>Quantity</th>
</tr>
</thead>
@foreach (var item in Model.SalesDataModelItems)
{
<tbody>
<tr>
<td>@item.UserCode</td>
<td>@item.SalesManName</td>
<td>@item.SaleDate</td>
<td>@item.ItemCode</td>
<td>@item.ItemDescription</td>
<td>@item.BrandCode</td>
<td>@item.BrandName</td>
<td>@item.ClientCode</td>
<td>@item.ClientName</td>
<td>@item.ClientBranchCode</td>
<td>@item.Description</td>
<td>@item.BranchSubChannel</td>
<td>@item.TrxAmount</td>
<td>@item.Quantity</td>
</tr>
</tbody>
}
</table>
</div>
</div>
</div>
</div>
}
</div>
</div>
</div>
<script>
$(function () {
$('#btnSave').on('click', function () {
$.ajax({
url: $(this).data('url'),
type: 'post',
datatype: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
console.log(data);
},
error: function (xhr) {
console.log(JSON.parse(xhr.responseText));
}
});
});
});
</script>