Я пытаюсь отправить объект списка обратно в метод действия контроллера из таблицы, чтобы экспортировать данные в формате csv.
Я перепробовал все средства, но не смог понять это, я был бы очень признателен, если бы кто-нибудь мне помог.
Дайте мне знать, если я сделал это правильно, cshtml и метод действия.
@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");
}