I have a webpage on which there are two drop down list selections. On submitting the page loads with a PDF viewer I've implemented using HTML object tag. Now the object tag calls the function on the controller to load the PDF file.
Code of the View:
<object data="@Url.Action("GetPDF")" type="application/pdf" width="100%" height="500"></object>
My controller:
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 link is static and I want to make it dynamic by:
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");
}
As I want to retrieve a specific file based on the selection from the dropdown lists, I've parameterized the function with Model with values from the View. But the Model's object doesn't capture any values.
I also use an ActionResult Submit which brings the form collected values to the controller:
[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);
}
How can I bring user selected values to the GetPDF function?