MVC3中的会话状态问题ASP.NET在本地工作但在发布到IIS8时不工作

时间:2015-03-25 15:53:31

标签: c# asp.net-mvc asp.net-mvc-3 session

我有一个应用程序,它在数据库上执行搜索并使用户能够更新某些字段。当用户从编辑视图返回到搜索视图时,我想显示原始搜索结果。我使用session来存储两个字符串,这些字符串被传递给我的Search操作来过滤搜索。当我在本地运行我的应用程序时,一切正常,但在我将应用程序发布到IIS8之后,在将更改保存到数据库并导航回搜索后,会话状态似乎被清除。

以下是我的Controller中的ActionResults:

public ActionResult Search(string searchString, string SearchOption) 
    {
        Session["searchString"] = searchString;
        Session["SearchOption"] = SearchOption;
        if (!String.IsNullOrEmpty(searchString))
        {
            searchString = searchString.Replace(" ", "");
            if (SearchOption == "PolicyNumber")
            {
                var vehicles = from v in db.Vehicles
                               where v.VehiclePolicyNumber == searchString
                               orderby v.VehiclePolicyNumber, v.DateVehicleAddedPolicy
                               select v;                    
                ModelState.Clear();
                return View(vehicles);
            }
            else
            {
                //Search for vehicle policy numbers
                var vehiclePolicies = from v in db.Vehicles
                               where v.RegistrationNumber == searchString
                               select v.VehiclePolicyNumber;

                //Search for vehicles with the above policy numbers
                var vehicles = from v in db.Vehicles
                                      where vehiclePolicies.Contains(v.VehiclePolicyNumber)
                                      orderby v.VehiclePolicyNumber, v.DateVehicleAddedPolicy
                                      select v;                    
                ModelState.Clear();
                return View(vehicles);
            }
        }
        else 
        {                
            var vehicles1 = from v in db.Vehicles
                            where v.VehicleMake == "g"
                            select v;
            return View(vehicles1);                
        }          
    }        

    public ActionResult editOnDate(string id)
    {
        var onDateToEdit = (from Vehicle in db.Vehicles where Vehicle.VehicleId.Equals(id) select Vehicle).First();            
        return View(onDateToEdit);
    }

    [HttpPost]
    public ActionResult editOnDate(string id, Vehicle vehicle) 
    {
        var onDateToEdit = (from Vehicle in db.Vehicles where Vehicle.VehicleId.Equals(id) select Vehicle).First();
        onDateToEdit.VehicleOnDate = vehicle.VehicleOnDate;
        db.SaveChanges();
        ViewBag.Message = "Saved!";
        return View(onDateToEdit);
    }

应使用此操作链接将会话数据传递到“搜索”视图:

 <%: Html.ActionLink("Back to Search", "Search", "Vehicle", new {searchString = Session["searchString"], SearchOption = Session["SearchOption"]}, new { @class = "barButton" })%>

一切都在本地运作,但令人沮丧的是它在发布时不起作用。任何帮助,将不胜感激。感谢

0 个答案:

没有答案