获取空参数C#MVC 4的操作

时间:2014-08-20 15:11:47

标签: c# asp.net-mvc asp.net-mvc-4 razor

当我通过我的代码调试时,我可以看到结果已存储数据但是当它到达结果操作时......结果为空。为什么会这样?默认操作获取正确的参数并提供正确的结果,但我无法将我的默认操作中的结果列表作为结果操作的参数传递。

[HttpGet]
public ActionResult Default()
{
    var currentUserCono = GetCurrentUserCono();

    //Set the Viewbags to populate the drop down menus on the view
    ViewBag.CompanyList = SelectListLib.GetCompanies(currentUserCono);
    ViewBag.BranchList = SelectListLib.GetBranches(currentUserCono);
    ViewBag.StatusTypeList = SelectListLib.GetStatusTypes();

    return View();
}

[HttpPost]
public ActionResult Default(int cono, string firstName, string lastName, string branch, string salesRep, bool statustype)
{
    //Query the Search Options with the database and return the matching results to the Results Page.
    var results = EmployeeDb.EmployeeMasters.Where(e => e.StatusFlag == statustype);

    results = results.Where(e => e.CompanyNumber == cono);
    if (!branch.IsNullOrWhiteSpace())
    {
        results = results.Where(e => e.Branch == branch);
    }
    if (!firstName.IsNullOrWhiteSpace())
    {
        results = results.Where(e => e.FirstName == firstName);
    }
    if (!lastName.IsNullOrWhiteSpace())
    {
        results = results.Where(e => e.LastName == lastName);
    }

    return RedirectToAction("Results", "Employee", routeValues: new { results = results.ToList() });

    // results has data in it 
}

[HttpGet]
public ActionResult Results(List<Models.EmployeeMaster> results)
{
    // results is equal to null
    return View(results);
}

1 个答案:

答案 0 :(得分:3)

这个问题已经回答in this StackOverflow question。 RedirectToAction不能作为普通方法调用,而是作为HTTP重定向(有关详细信息,请参阅其他链接)。您可以使用TempData或直接调用该方法:

[HttpPost]
public ActionResult Default(int cono, string firstName, string lastName, string branch, string salesRep, bool statustype)
{
    //Query the Search Options with the database and return the matching results to the Results Page.
    var results = EmployeeDb.EmployeeMasters.Where(e => e.StatusFlag == statustype);

    results = results.Where(e => e.CompanyNumber == cono);
    if (!branch.IsNullOrWhiteSpace())
    {
        results = results.Where(e => e.Branch == branch);
    }
    if (!firstName.IsNullOrWhiteSpace())
    {
        results = results.Where(e => e.FirstName == firstName);
    }
    if (!lastName.IsNullOrWhiteSpace())
    {
        results = results.Where(e => e.LastName == lastName);
    }

    return Results(results.ToList()); 
}