我正在尝试将列表对象从表中发布回控制器操作方法,以将数据导出为csv。
我尽了一切努力,但想不通,如果有人可以帮助我,我将不胜感激。
让我知道我是否在cshtml和action方法上做得正确。
@model List<RapidFinanceCodeBehindBusinessLogic.ViewModels.SharedViewModel>
@{
ViewBag.Title = "GetCompaniesFromUploadedFile";
//var modelOrdered = Model.OrderByDescending(m=>m.CompanyName);
}
<h2>Get Companies From Uploaded File</h2>
@using (Html.BeginForm("ExportCompaniesListToCSV", "Home"))
{
<table class="table table-bordered">
<thead>
<tr>
@*<th>
@Html.ActionLink(Model.OrderByDescending(x => x.CompanyName))
</th>*@
<th><a>Company Name</a></th>
<th><a>Years in Business</a></th>
<th><a>Contact Name</a></th>
<th><a>Contact Phone Number</a></th>
<th><a>Contact Email</a></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.CompanyName</td>
<td>@item.YearsInBusiness</td>
<td>@item.ContactName</td>
<td>@item.ContactPhoneNumber</td>
<td>@item.ContactEmail</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Export to CSV" class="btn btn-success" />
</div>
</div>
}
[HttpPost]
public ActionResult ExportCompaniesListToCSV(FormCollection model)
{
StringWriter sw = new StringWriter();
sw.WriteLine("\"Company Name\",\"Years in Business\",\"Contact Name\",\"Contact Phone Number\",\"Contact Email\"");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=Exported_Users.csv");
Response.ContentType = "text/csv";
//foreach (var line in model)
//{
// sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"",
// line.FirstName,
// line.LastName,
// line.Dob,
// line.Email));
//}
Response.Write(sw.ToString());
Response.End();
return RedirectToAction("GetCompaniesFromUploadedFile");
}
答案 0 :(得分:0)
我不得不利用asp.net MVC概念,例如临时数据,视图数据,将数据从一个控制器动作传递到另一个控制器动作。整个想法不是使用javascript库和引导程序,因此使用临时数据和视图数据来实现此目的。 感谢社区的所有支持。