0

Actually I am having a requirement like :

I need to get the ID's of checked records by using Javascript and store the id's in a cookie and access that cookie in controller and delete the records based on that id's

My Javascript is :

<script type="text/javascript">
    //<![CDATA[

    function SelectionChanged(s, e)
    {

        s.GetSelectedFieldValues("ID", GetSelectedFieldValuesCallback);
    }
    function GetSelectedFieldValuesCallback(values)
    {
        SelectedRows.BeginUpdate();
        try
        {
            SelectedRows.ClearItems();
            var s = '';
            for (var i = 0; i < values.length; i++) {
                //SelectedRows.AddItem(values[i]);
                s=s+values[i]+',';

            }
        }
        finally
        {
            document.cookie = s;
        }
        $("#count").html(gvRowSelection.GetSelectedRowCount());

    }
    // ]]>

2 Answers 2

4

I'm not exactly sure what your question is specifically, but if you're looking for a way to set cookies via Javascript, check out the jquery-cookie plugin. You'd access it in your controller just like you would any other cookie:

Javascript

$.cookie('mycookie', 'myvalue');

C#

Request.Cookies["mycookie"].Value;
Sign up to request clarification or add additional context in comments.

3 Comments

Do i need to add the plugin in my page ?
Yes, download the jquery.cookie.js file from the GitHub repository I linked to, add it to your MVC project and then include it with <script type="text/javascript" src="~/Scripts/jquery.cookie.js"></script> or wherever you placed the script file.
I am not able to access that cookie in controller.Its getting Object reference not set to an instance of an object
0

I think Scott was rigth except that you need to Base64 decode the data then JSON decode the data.

JavaScript

$.jCookies({name:'user',value:{name:'brian',level:'awesome'}});

C#

Convert.FromBase64String(Page.Request.Cookies["user"].Value)

For more information look at the following Page: Here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.