views:

347

answers:

1

Hi,

i'm trying to set a cookie to store a selected department in. The cookie is set with a small form which has a select-dropdown with departments. This is posted using AJAX.

This is how I store the 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

The .newDepartment variable is returned correctly with the correct value.

This is how I retrieve the cookie and build the select-dropdown:

<% 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%>

The cookie isn't stored, because the select-dropdown keeps going back to Default. Am I doing something wrong?

+1  A: 

You could try to use http debugger like Fiddler for IE and HttpFox for FireFox. Check if the cookie is really send with the server response and if the browser sends it back with further requests. From there you are probably going to track the problem and see what is going wrong. If you still have problems you could post the http traffic here.

devdimi
Ok that gave me some more info. It turns out i was clearing the cookie value where I was getting the value. But somehow the expires-value is ALWAYS end-of-session. Response.Cookies("department").expires = Now.AddDays(7) doesn't seem to get through?
Ropstah