I have a form where a user can put in a date and view a table of data depending on the date. If the user wants to, he can download the data as a CSV file. This is my controller:
public class SomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
ModelClass model = new ModelClass();
return View(model);
}
[HttpPost]
public ActionResult Index(ModelClass data)
{
if (data != null)
{
data.ReadData();
return View(data);
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
}
[HttpPost]
public FileContentResult DownloadCSV(ModelClass data)
{
if (data != null)
{
// generate CSV
}
else
{
// ...
}
}
}
Basicly my GET-Index generates an empty model with default values (default date and data = null). Then via a form the user changes the date and submits the model to the POST-Index. There the function ReadData
is executed, which generates the data within the model from a database-request and displays it in the View. If data is not null in the model the View displays a table with another form, which links to DownloadCSV
. I would expect that the same model with my data would be posted to the DownloadCSV
-site. But instead i get an empty model, which is not null, but without data. How can i accomplish my desired behavior and why is the described behavior to be expected?
Here is the View:
@using System.Data
@model Project.Models.ModelClass
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Title</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SubTitle</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
<div class="input-group date">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control form-date datecontrol", id = "datecontrol1" } })
<label class="input-group-addon btn" for="datecontrol1">
<i class="glyphicon glyphicon-calendar"></i>
</label>
</div>
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Submit" class="btn btn-default" />
</div>
</div>
</div>
}
@if (Model.Data != null)
{
<hr />
<div class="pre-scrollable">
<table class="table table-bordered table-responsive table-hover">
<tr>
<th style="white-space: nowrap; width: 1%;">Zeit</th>
@foreach (DataColumn col in Model.Data.Columns)
{
<th>@col.ColumnName</th>
}
</tr>
@foreach (DataRow row in Model.Data.Rows)
{
<tr>
@foreach (DataColumn field in Model.Data.Columns)
{
if (field.ColumnName != "Zeit")
{
<td>@(row[field.ColumnName] != DBNull.Value ? Convert.ToDouble(row[field.ColumnName]).ToString("F1") : "0")</td>
}
}
</tr>
}
</table>
</div>
using (Html.BeginForm("DownloadCSV", "Some"))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-offset-9 col-md-12">
<input type="submit" value="Export CSV" class="btn btn-default" />
</div>
</div>
}
}
@*<div>
@Html.ActionLink("Back to List", "Index")
</div>*@
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
And the model:
public class ModelClass
{
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
public DataTable Data { get; private set; }
public ModelClass()
{
Date = DateTime.Now;
}
public void ReadData()
{
Data = new DataTable();
// ... read data from database into datatable
}
}
As you can see, Data
should only be null if ReadData
wasn't executed. So what i get in DownloadCSV
is a just created instance of ModelClass
.
Update:
I could solve the transfer of the Data
field from the POST-Index
to DownloadCSV
without requesting the data again using TempData
following this question: C# MVC pass data from one action method to another