2009-07-06 51 views
1

我試圖設置一個cookie來存儲一個選定的部門。該cookie設置了一個小表單,其中有一個與部門選擇下拉菜單。這是使用AJAX發佈的。ASP.NET - 爲什麼我的cookie不會持久?

這是我如何保存的Cookie:

<AcceptVerbs(HttpVerbs.Post)> _ 
Function ChangeDepartment(ByVal FormValues As FormCollection) As ActionResult 
    If Response.Cookies("department") IsNot Nothing Then 
     Response.Cookies("department").Value = FormValues("department") 
    Else 
     Dim c As New HttpCookie("department") 
     c.Value = FormValues("department") 
     c.Expires = Now.AddDays(7) 
     Response.Cookies.Add(c) 
    End If 

    Return Json(New With {.newDepartment = Response.Cookies("department").Value}) 
End Function 

.newDepartment變量與正確的值返回正確。

這是我如何檢索Cookie,並構建選擇,下拉菜單:

<% Ajax.BeginRouteForm("ChangeDepartment", New AjaxOptions With {.LoadingElementId = "loading", .HttpMethod = "post", .OnSuccess = "function(request) {ajaxMessage('Department change', 'Department changed to: ' + request.get_response().get_object().newDepartment);}"})%> 
    <select name="department"> 
     <option>Default</option> 
     <option<%If Request.Cookies("department") Isnot Nothing andAlso Request.Cookies("department").Value = "Supervisor" Then Response.Write (" selected=""selected""") %>>Supervisor</option> 
     <option<%If Request.Cookies("department") Isnot Nothing andAlso Request.Cookies("department").Value = "Purchasing" Then Response.Write (" selected=""selected""") %>>Purchasing</option> 
     <option<%If Request.Cookies("department") Isnot Nothing andAlso Request.Cookies("department").Value = "Engineering" Then Response.Write (" selected=""selected""") %>>Engineering</option> 
    </select> 
    <input type="submit" value="Change department" /> 
<% Html.EndForm%> 

cookie不會保存,因爲選擇,下拉一直追溯到Default。難道我做錯了什麼?

回答

1

您可以嘗試使用http調試器,如Fiddler for IE和HttpFox for FireFox。 檢查cookie是否確實是與服務器響應一起發送的,並且瀏覽器是否將它發送回來還有其他請求。 從那裏你可能要跟蹤問題,看看發生了什麼問題。如果你仍然有問題,你可以在這裏發佈http流量。

+0

好吧,給了我一些更多的信息。事實證明,我正在清除cookie價值的地方。但不知何故,過期值總是會話結束。 Response.Cookies(「department」)。expires = Now.AddDays(7)似乎沒有通過? – Ropstah 2009-07-06 10:24:29