我的观点中有一个表格:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.DateFrom, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateFrom, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.DateFrom, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateTo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateTo, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.DateTo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" formAction=@Url.Action("CreateReport") />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EMail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EMail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EMail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Send to email" class="btn btn-default" formAction=@Url.Action("SendEmail") />
</div>
</div>
</div>
}
正如你所看到我有两个按钮,第一个按钮调用CreateReport动作而不是发送按钮调用SendEmail动作。我想创建报告,然后通过电子邮件发送此报告。
这是我的控制器操作:
public ActionResult Index()
{
Report
report=ReportRepository.GetReport(DateTime.Parse("02.08.1996"), DateTime.Parse("07.08.1996"));
return View(report);
}
public ActionResult CreateReport(Report report)
{
report = ReportRepository.GetReport(report);
return View("Index", report);
}
public ActionResult SendEmail(Report report)
{
return View("Index", report);
}
我的模特课:
public class Report
{
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public List<OrderDetails> Orders { get; set; }
[Display(Name = "Email address")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string EMail { get; set; }
}
所以我的意思是我在CreateReport操作中填写Orders列表并显示它,然后我按下&#34;发送到电子邮件&#34;按钮,那个电话&#34; SendEmail&#34;动作,我将订单列表保存到文件并发送。
问题在于&#34; SendEmail&#34;动作列表为空。 我该如何解决?
答案 0 :(得分:0)
我能想到的最简单的方法是删除创建报告的提交操作,并使用ajax调用处理此操作。这样你只需要一次提交动作。
否则,您可以在视图中尝试使用2个表单。
就个人而言,我更喜欢第一种选择。
答案 1 :(得分:0)
我找到了解决方案。解决方案不是将模型传递给控制器,而是将我的List存储在Session中。像这样:
public ActionResult Index()
{
Report report=ReportRepository.GetReport(DateTime.Parse("02.08.1996"), DateTime.Parse("07.08.1996"));
Session["Orders"] = report.Orders;
return View(report);
}
public ActionResult CreateReport(Report report)
{
report = ReportRepository.GetReport(report);
Session["Orders"] = report.Orders;
return View("Index", report);
}
public ActionResult SendEmail(Report report)
{
List<OrderDetails> orders = (List<OrderDetails>)Session["Orders"];
report.Orders = orders;
return View("Index", report);
}