我有一个表单,用户可以根据日期输入日期并查看数据表。如果用户想要,他可以将数据下载为CSV文件。 这是我的控制者:
ReadData
基本上我的GET-Index会生成一个带有默认值的空模型(默认日期和数据= null)。然后通过表单更改日期并将模型提交到POST-Index。在那里执行函数DownloadCSV
,它从数据库请求生成模型中的数据并将其显示在视图中。如果模型中的数据不为空,则View会显示另一个表单,该表格链接到DownloadCSV
。我希望我的数据的相同模型将发布到@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")
}
- 网站。但相反,我得到一个空模型,它不是空的,但没有数据。我如何实现我想要的行为?为什么所描述的行为是预期的?
以下是视图:
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
}
}
模特:
Data
如您所见,ReadData
只有在DownloadCSV
未执行时才应为空。所以我在ModelClass
中获得的是刚创建的Data
实例。
我可以解决Index
字段从POST DownloadCSV
转移到TempData
而无需使用allow write: if resource.data.members.data[(request.auth.token.email)] in ["admin"];
再次请求数据的问题:{{3} }
答案 0 :(得分:0)
感谢Stephen Mueckes的评论和这个问题 C# MVC pass data from one action method to another 我可以通过以下方式解决问题:
public class SomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
ModelClass model = new ModelClass();
return View(model);
}
[HttpPost]
public ActionResult Index(ModelClass model)
{
model.ReadData();
TempData['data'] = model.Data
return View(data);
}
[HttpPost]
public FileContentResult DownloadCSV(ModelClass model)
{
// model only has the date populated from the form, so take the data from TempData
model.Data = TempData['data']
// ... generate CSV
}
}
using (Html.BeginForm("DownloadCSV", "Some"))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-offset-9 col-md-12">
@Html.HiddenFor(model => model.Date)
<input type="submit" value="Export CSV" class="btn btn-default" />
</div>
</div>
}
这样,我将结果存储在TempData
中,并且在导出CSV时不必再次请求它们。 TempData
只会存储以供下列请求使用。