如何保存视图模型以进行回发查看?

时间:2014-11-18 05:16:17

标签: c# asp.net-mvc post viewmodel

我正在开发MVC项目,我已成功创建了一个包含多个下拉列表和文本框的搜索页面。用户查询数据后,将它们传输到List页面,其中包含与我们的搜索相对应的结果列表。 我想知道是否可以创建位于列表视图上的按钮将其返回到搜索页面,搜索页面的文本框/下拉列表仍然填充了之前的搜索。目前,当他们返回时,他们之前的搜索被清除,他们无法看到被查询的内容。

简化:有没有办法在查询提交时捕获视图模型的数据,然后能够通过简单的返回按钮访问它。

获取方法(填充下拉列表等)

[HttpGet]
public ActionResult Index()
{
    //testTypes
    var testTypesL = new List<string>();
    var testTypesQr = from z in db.Results
                      orderby z.TestType
                      select z.TestType;
    testTypesL.AddRange(testTypesQr.Distinct());

    //technicians
    var techniciansL = new List<string>();
    var techniciansQr = from z in db.Results
                        orderby z.Technician
                        select z.Technician;
    techniciansL.AddRange(techniciansQr.Distinct());

    //engineers
    var engineersL = new List<string>();
    var engineerQr = from z in db.Results
                     orderby z.Engineer
                     select z.Engineer;
    engineersL.AddRange(engineerQr.Distinct());

    //testStalls
    var testStallL = new List<string>();
    var testStallQr = from z in db.Results
                      orderby z.TestStall
                      select z.TestStall;
    testStallL.AddRange(testStallQr.Distinct());

    //unit models
    var unitModelL = new List<string>();
    var unitModelQr = from z in db.Results
                      orderby z.UnitID
                      select z.UnitID;
    unitModelL.AddRange(unitModelQr.Distinct());

    TestDataViewModel obj = new TestDataViewModel();
    obj.EngineerList = new SelectList(engineersL);
    obj.TechnicianList = new SelectList(techniciansL);
    obj.TestStallList = new SelectList(testStallL);
    obj.UnitModelList = new SelectList(unitModelL);
    obj.TestTypeList = new SelectList(testTypesL);

    return View("Index", obj);
}

发布方法:(将用户查询发送到列表视图)

[HttpPost]
public ActionResult Index(TestDataViewModel obj)
{
    var data = from d in db.Results                 
               select d;
    //search data parameters

    if (!String.IsNullOrEmpty(obj.StartDate.ToString()) && !String.IsNullOrEmpty (obj.EndDate.ToString()))
    {
        data = data.Where(z => obj.StartDate <= z.EndDate && obj.EndDate >= z.EndDate);            
    }

    if (!String.IsNullOrEmpty(obj.TestNumber.ToString()))
    {
        data = data.Where(z => z.TestNumber == obj.TestNumber);              
    }

    if (!String.IsNullOrEmpty(obj.unitModel))
    {
        data = data.Where(z => z.UnitID == obj.unitModel);
    }

    if (!String.IsNullOrEmpty(obj.Project))
    {
        data = data.Where(z => z.ProjectNum.Contains(obj.Project));           
    }

    if (!String.IsNullOrEmpty(obj.testType))
    {
        data = data.Where(z => z.TestType == obj.testType);              
    }
    if (!String.IsNullOrEmpty(obj.engineer))
    {
        data = data.Where(z => z.Engineer == obj.engineer);              
    }
    if (!String.IsNullOrEmpty(obj.technician))
    {
        data = data.Where(z => z.Technician == obj.technician);               
    }
    if (!String.IsNullOrEmpty(obj.testStall))
    {
        data = data.Where(z => z.TestStall == obj.testStall);              
    }

    return View("List", data);
}

0 个答案:

没有答案