0
  1. what I would like to do is when the first time app will load I will store Menu in client-side sessionStorage and next time any page load get menu from sessionStorage
  2. My website flow is LoginPage > Dashboard page
  3. Dashboard page renders as index.csHtml and uses _Layout.csHtml partial view to the reader
  4. I successfully store sessionStorage value on client-side but I am not able to get this save value in _Layout.csHtml Page.
  5. In _Layout.csHtml body part this code use @{Html.RenderAction("RenderTopMenu", "Common");} and I need to pass sessionStorage value in this RenderAction EX: @{Html.RenderAction("RenderTopMenu", "Common", new {model=sessionStorage});}
PAGE - Index.csHtml 

@{
   ViewBag.Title = "Welcome To " + SessionManagement.CompanyName;
   Layout = (ViewBag.isPartialView) ? null : "~/Views/Shared/_Layout.cshtml";
}

PAGE - _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
</head>
<body class="pageContent">

   @{Html.RenderAction("RenderTopMenu", "Common");}
</body>

PAGE - Controller
public ActionResult RenderTopMenu(DashboardViewModel AdminMenus)
       {
           DashboardViewModel Dashboardmodel = new DashboardViewModel();
           Dashboardmodel = DashboardModel.GetTopMenu(Dashboardmodel);
}

PAGE - Model

public List<AdminMenus> GetMenuListFromCache(int AccountMode, DashboardViewModel Dashboardmodel)
       {
List<AdminMenus> AdminMenusList = sessionStorage.getitem("Menu") as List<AdminMenus>;


                   if (AdminMenusList == null) //not in cache
                   {
                       AdminMenuParamsModel menus = new AdminMenuParamsModel()
                       {
                           CompanyID = SessionManagement.IntCompanyID,
                           MenuID = AccountMode
                       };

                       AdminMenusList = new DashboardBLL(SessionManagement.DevPasswordConnectionString).ProcessGetMenus(menus);
                       AdminMenusList = DashboardModel.SetMenuName(AdminMenusList);
                       sessionStorage.setitem("Menu") = AdminMenusList;
                   }
                   return AdminMenusList;
}

NOTE: In a model for only explanation purpose I put sessionStorage.getitem but really I can not use this.
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Anand Gajjar
  • 1
  • 1
  • 2

2 Answers2

1

you can not access javascript values from razor, i would recommend coding your Menu using jquery or javascript or you could save your menu object on the session on the server side and pass it in the viewbag and then use razor.

How to pass a value to razor variable from javascript variable?

a.tolba
  • 137
  • 1
  • 1
  • 13
0

In this line @{Html.RenderAction("RenderTopMenu", "Common", new {model=sessionStorage});} You just have to use

@{Html.RenderAction("RenderTopMenu", "Common", new {model=sessionStorage.Menu});} or @{Html.RenderAction("RenderTopMenu", "Common", new {model=sessionStorage.getitem("Menu")});} ideally it should works.

  • sessionStorage.getitem("Menu") only access by JavaScript or jQuery....we can not access it from mvc @Html.RenderAction property – Anand Gajjar Oct 19 '19 at 14:25