Session variable issue

May 10, 2013 at 9:36 AM
Edited May 10, 2013 at 9:39 AM
Dear Friends,

I am having an issue while setting session variables in nearforums. I am trying to set session variable values through Ajax call using JQuery as follows. But when I try to access then in some action method, their values becomes null.

Here's how I am setting them through ajax call in a Action method in TopicsController:

base.Session.SessionVariale = "Hello world";
or
HttpContext.Session["SessionVariale"] = "Hello world";

And here's how I am trying to access them in another action method (non-Ajax call) in the same controller (TopicsController):

string xyz = base.Session.SessionVariale;
or
string xyz = HttpContext.Session["SessionVariale"];

but both of them gives null value. Somewhere they are getting set to null. Can you please let me know some solution for this.

Thanks & Regards
Suresh.
Coordinator
May 10, 2013 at 1:41 PM
Hi!
Is the rest of the session data being stored? Like authentication / user data...
If you are logged in, do you get logged off?

Regards!
May 10, 2013 at 1:53 PM
Hi Jorge..
Rest of data is getting stored as I can see base.Session.CaptchaHash value and authentication
Data is also getting stored as I am not getting logged out.
But different is that I am setting my session variables by using $.post jquery call..when debugged jsonresult method..value is set properly..but on button click, when add action method of topics controller is called then these variables which are set by me in ajax request gets null value.
Not able to understand the cause..please guide.
May 11, 2013 at 7:01 AM
Dear Jorge,

Let me give you more details which might help you trace out exact cause.
On create a new thread page I am trying to implement image upload functionality with the help of swf uploadify. Here is the script for uploading image:
<script type="text/javascript">
    $(function () {
        $('#file_upload').uploadify(
            {
                'swf': '/uploadify.swf',
                'uploader': '/topic/imageupload',
                'checkExisting': '/topic/checkexisting',
                'cancelImg': 'cancel.png',
                'fileTypeExts': '*.gif; *.jpg; *.png; *.bmp; *.jpeg; *.JPG; *.JPEG; *.PNG;*.GIF; *.BMP;',
                'method': 'post',
                'file_size_limit': '1 MB',
                'file_upload_limit': 5,
                'onUploadSuccess': function (file, data, response) {
                    //alert(file, data, response);
                    var responseData = jQuery.parseJSON(data);
                    var upload_url_tmp = responseData["src"];
                    OnImageUploadSuccess(upload_url_tmp);
                },
                'onUploadError': function (file, errorCode, errorMsg, errorString) {
                    alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
                }
            });
        //$('#file_upload').uploadify('settings', 'ResetUploadsSuccessful', '0');
    });
</script>
This is calling imageupload action method in topic controller, where I am setting session variables which will hold uploaded image name/path which I can use in Add action method of topic controller used for creating a new thread.

Here is how I am setting session variables:
public ActionResult ImageUpload(System.Web.HttpPostedFileBase fileData)
        {
            string filePath = String.Empty;

            if (fileData != null)
                filePath = SaveUploadedFile(fileData);

           base.Session.Image1 = filePath;
Or
           HttpContext.Session["image1"] = filePath;
            
            filePath = "<img src='" + filePath + "' height='90' width='110'/>";
            return Json(new { src = filePath });
        }
Now these session variable values I need to access in Add action method, But their values are null.
Can you please tell me where I am going wrong.

Thanks a lot for your kind help.

Best Regards
Suresh