Question

Is it possible to set the session value in jquery and get that value in controller post method without using any ajax call. I am trying to set one list value to session in jquery

$.session.set('SelectedIdList', selectedIds);

now a want to get these value in Controller post method

public ActionResult SaveTransactions(BusinessViewModel model, string Submit)
{

  model.SelectedIdsList = (string[])Session["SelectedIdList"];

}

Was it helpful?

Solution 3

I have removed the session from my code I have taken one hidden field and looping that and storing the value with ',' which I am getting that in controller

      $("#hdnSelectedIdList").val("");
      for (var iCount = 0; iCount < SelectedIdList.length; iCount++) {
            $("#hdnSelectedIdList").val($("#hdnSelectedIdList").val() + ',' + SelectedIdList[iCount]);
        }

@Html.HiddenFor(m=>m.hdnSelectedIdList)

and in controller

 model.SelectedIdsList = model.hdnSelectedIdList[0].Split(',');

OTHER TIPS

you cannot set in jquery as Session is a server side thing.

you have to send an ajax call if it is really needed like this:

$.ajax({

                url: '@Url.Action("SetSession","Controller")',
                data {session:1},
                success: function (response) {

                },
                error: function () {

                    alert("Error occured!");

                }

            });

in Action:

[HttpGet]
public Action Result SetSession(string session)
{
Session["key"] = session;
}

You can't set and retrieve like this, since both the data are maintained in a different place. One is in browser and another one is in server.

From $.session Plugin Author

Provides a simple interface into window.sessionStorage while providing limited support for cross protocol requests.

ASP.Net Session

This is basically maintained in the server side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top