如何在Controller中从View到FileStreamResult函数获取Form Collected值

时间:2017-06-15 08:07:37

标签: asp.net-mvc asp.net-mvc-4

我有一个网页,其中有两个下拉列表选项。在使用PDF查看器提交页面加载时,我已使用HTML object 标记实现。 现在 object 标记调用控制器上的函数来加载PDF文件。

观点代码:

<object data="@Url.Action("GetPDF")" type="application/pdf" width="100%" height="500"></object>

我的控制器:

  public FileStreamResult GetPDF()
    {
      FileStream fs = new FileStream("C:/Users/ABC/Desktop/Office/Resources/Resources/HRDocs/2017/May/2017-MAY-1887CJODU5VKJM.pdf", FileMode.Open, FileAccess.Read);
        return File(fs, "application/pdf");
    }

FileStream链接是静态的,我希望通过以下方式使其成为动态:

     public FileStreamResult GetPDF(HRModel objHR)
    {
        string UserName = User.Identity.Name.ToString();
        string SelectedYr = Convert.ToString(objHR.SelectedYear);
        int SelectedMonth = Convert.ToInt32(objHR.SelectedMonthId);
        string MonthName = dbContext.EmployeeSalaries.Where(f => f.MonthId == objHR.SelectedMonthId).FirstOrDefault().Month;
        var SalLink = dbContext.EmployeeSalaries.FirstOrDefault(a => a.MonthId == SelectedMonth && a.Year == SelectedYr && a.UserName == UserName);

      FileStream fs = new FileStream(Server.MapPath(string.Format("~/HRDocs/{0}/{1}/{2}", objHR.SelectedYear, MonthName, SalLink.URL)), FileMode.Open, FileAccess.Read);
        return File(fs, "application/pdf");
    }

由于我想根据下拉列表中的选择检索特定文件,因此我使用View中的值对Model进行参数化。 但模型的对象并没有捕获任何值。

我还使用ActionResult Submit将表单收集的值带到控制器:

    [HttpPost]
    public ActionResult Submit(FormCollection frmCollection, HRModel objHR)
    {

        try
        {
            string UserName = User.Identity.Name.ToString();
                ViewBag.UserId = User.Identity.Name.ToString();
                objHR.YearList = dbContext.EmployeeSalaries.Where(f => f.UserName == UserName).GroupBy(f => f.Year).Select(g => g.FirstOrDefault()).ToList();
                objHR.MonthList = dbContext.EmployeeSalaries.Where(f => f.UserName == UserName).GroupBy(f => f.MonthId).Select(g => g.FirstOrDefault()).ToList();
                string SelectedYr = Convert.ToString(objHR.SelectedYear);
                int SelectedMonth = Convert.ToInt32(objHR.SelectedMonthId);
                string MonthName = dbContext.EmployeeSalaries.Where(f => f.MonthId == objHR.SelectedMonthId).FirstOrDefault().Month;
                var SyncDate = dbContext.EmployeeSalaries.FirstOrDefault(a => a.MonthId == SelectedMonth && a.Year == SelectedYr && a.UserName == UserName);
                objHR.SyncDate = Convert.ToDateTime(SyncDate.SyncDate);
                var SalLink = dbContext.EmployeeSalaries.FirstOrDefault(a => a.MonthId == SelectedMonth && a.Year == SelectedYr && a.UserName == UserName);
                if (SalLink == null)
                {
                    ViewBag.Empty = "true";
                }
                else
                {
                    ViewBag.Links = string.Format("/HRDocs/{0}/{1}/{2}", objHR.SelectedYear, MonthName, SalLink.URL);
                    objHR.URL = SalLink.URL;
                    ViewBag.IsValid = "true";
                }
                return View(objHR);
            }

如何将用户选择的值带入GetPDF函数?

1 个答案:

答案 0 :(得分:0)

您必须意识到呈现页面的请求和获取PDF以在该页面上呈现的请求是两个单独的请求。

所以这个should just work,授予您将适当的参数传递给@Url.Action()

<object data="@Url.Action("GetPDF", new { SelectedYear = "2017", SelectedMonthId = "6" })" ... />

然后HRModel GetPDF()模型参数将从该操作的查询字符串绑定。

如果您想在下拉菜单更改时更改客户端的URL,那么这完全是一个完全不同的问题。请参阅示例ASP.net mvc Call Action on DropDown Value Change