【问题标题】:One model for two submit buttons in ASP.NET MVCASP.NET MVC 中两个提交按钮的一种模型
【发布时间】:2017-07-27 05:18:33
【问题描述】:

我的视图中有一个表单:

@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 操作中填写订单列表并显示它,然后按“发送到电子邮件”按钮,这就是所谓的“SendEmail”操作,我将订单列表保存到文件并发送。

问题是在“SendEmail”操作中列表为空。 我该如何解决?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 submit-button


    【解决方案1】:

    我能想到的最简单的方法是删除创建报告的提交操作,并使用 ajax 调用来处理。这样您就只有一个提交操作。

    或者,您可以尝试在视图中使用 2 个表单。

    就个人而言,我更喜欢第一种选择。

    【讨论】:

      【解决方案2】:

      我找到了解决方案。解决方案不是将模型传递给控制器​​,而是将我的列表存储在 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);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-19
        • 2011-09-17
        • 2016-08-02
        • 1970-01-01
        • 2013-08-31
        相关资源
        最近更新 更多