Send (Pass) Session value from View to Controller in ASP.Net MVC

Last Reply 5 months ago By dharmendr

Posted 5 months ago

Hi,

I have one table with fields    

 EId      Id    ControllerName         Url

1          1      report                      /report/Index/  

2           2       report1                   /eport1/Index/ 

assume session will be 1

then how to pass this id to controller

      public JsonResult getAll(int Id)
      {
          db.Configuration.ProxyCreationEnabled = false;
          using (ReportEntities dataContext = new ReportEntities())
          {
              var employeeList = (from E in dataContext.MenuSelection1
                                  where E.EId==Id
                                  orderby E.Id
                                  select new
                                  {
                                      E.Id,
                                      E.ControllerName,
                                      E.Url
                                  }).ToList();
              var JsonResult = Json(employeeList, JsonRequestBehavior.AllowGet);
              JsonResult.MaxJsonLength = int.MaxValue;
              return JsonResult;
          }
      }

 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
   $(function () {
       var Id1 = 1;//var Id1=@Session["EId"]
        $.ajax({
           type: "POST",
           url: "/menuselection/getAll/",
           contentType: "application/json; charset=utf-8",
           data: JSON.stringify({ Id: Id1}),
 
           dataType: "json",
           success: function (r) {
               var ul = $('#unordered');
               $.each(r, function (i, item) {
                   ul.append("<li><a href='" + item.Url + "'>" + item.ControllerName + "</a></li>");
               });
           }
       });
   });
</script>

 

You are viewing reply posted by: dharmendr 5 months ago.
Posted 5 months ago

Hi mahesh213,

Session is used to store data on the server. It allows a controller to pass data to another controller.

If you want to change a session value with jQuery, you must make an ajax call to an action that updates session.

You can access the Session value in Controller without passing it through Ajax request.

public JsonResult getAll()
{
    int Id = Convert.ToInt32(Session["EId"]);
    db.Configuration.ProxyCreationEnabled = false;
    using (ReportEntities dataContext = new ReportEntities())
    {
        var employeeList = (from E in dataContext.MenuSelection1
                            where E.EId == Id
                            orderby E.Id
                            select new
                            {
                                E.Id,
                                E.ControllerName,
                                E.Url
                            }).ToList();
        var JsonResult = Json(employeeList, JsonRequestBehavior.AllowGet);
        JsonResult.MaxJsonLength = int.MaxValue;
        return JsonResult;
    }
}

And remove the parameter from Ajax call.