【问题标题】:Passing value to another action result将值传递给另一个动作结果
【发布时间】:2014-03-05 07:48:48
【问题描述】:

我在此操作中收到 Emplid,(从包含提交 btn 和下拉的 DDL 操作调用)

public ActionResult showDDL(int? EmplID = null) 
        {
            ViewBag.EmplID = EmplID;
            if (EmplID == null) 
            {
                IEnumerable<GetAtdRecord_SpResult> EmployeeAtd_2 = DataContext.GetAtdRecord_Sp(0).ToList();
                return View(EmployeeAtd_2);
            }
            else if (EmplID != null) 
            {
                IEnumerable<GetAtdRecord_SpResult> EmployeeAtd_2 = DataContext.GetAtdRecord_Sp(EmplID).ToList();
                return View(EmployeeAtd_2);
            }

            return View();
        }

查看:

@{

     var grid = new WebGrid(ViewData.Model, defaultSort: "EmplID", rowsPerPage: 20);

  }

@if (Model.Count > 0)
{
  <div id="AllEmpGrid_ByName">
   @grid.GetHtml(columns: grid.Columns(
                                        grid.Column("EmplID", "Employee ID"),
                                        grid.Column("EmplName", "Employee Name"),
                                        grid.Column("ShiftID", "Shift ID"),
                                        grid.Column("DateVisited", "Date of Visit"),
                                        grid.Column("InTime", "In Time"),
                                        grid.Column("TimeOut", "Time Out"),
                                        grid.Column("OverTime", "Over Time"),
                                        grid.Column("TotalWorkingTime", "Total Working Time")
                                      ))
 </div>

 using (Html.BeginForm("ToExcel", "Home", FormMethod.Get))
 {
   <button type="submit" class="button_form button_download" >Download in Excel</button>
 }

}
else
{
    <h2 class="error" >No Data Found</h2> 
}

在同一个视图中,您可以看到 EXCEL 中的“下载”按钮,我想将此 emplID 传递给 ToExcel 方法

public ActionResult ToExcel(int? empid )
        {
            var DataContext = new EmployeeRecordDataContext();

            var grid = new GridView();
            grid.DataSource = DataContext.GetAtdRecord_Sp(null).ToList();
            grid.DataBind();

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=AttendanceSheet.xls");
            Response.ContentType = "application/ms-excel";

            Response.Charset = "";
            StringWriter sw = new StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            grid.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            return RedirectToAction("index");
        }

当我单击“在 Excel 中下载”按钮时,我无法弄清楚如何将操作“showDDL”中收到的 EmplID 传递给 EmpID?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 c#-4.0 razor


    【解决方案1】:

    您可以使用 ViewBag 执行此操作。首先将值存储在 ViewBag 中。

    public ActionResult showDDL(int? EmplID = null) 
    {
       ViewBag.Id = EmplId;
       // You other logic....
    }
    

    然后像这样在你的表单上使用它 -

     using (Html.BeginForm("ToExcel", "Home", new { empid = ViewBag.Id }, FormMethod.Get))
     {
       <button type="submit" class="button_form button_download" >Download in Excel</button>
     }
    

    还有一台服务器,您可以在参数中获取值 -

    public ActionResult ToExcel(int? empid)
    {
        // You logic and use empid here...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 2015-12-23
      • 2022-01-02
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      相关资源
      最近更新 更多