将值传递给另一个操作结果

时间:2014-02-07 18:50:37

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

我在这个动作中接收了Emplid,(从DDL动作中调用,包含提交btn和下拉)

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> 
}

在同一个视图中你可以看到,按钮DOWNLOAD IN 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");
        }

我无法弄清楚如何传递EmplID在Action&#39; showDDL&#39;单击按钮&#34;在Excel中下载&#34;到EmpID ?

1 个答案:

答案 0 :(得分:0)

你可以使用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...
}