0

So here goes another MVC question from a noob:

This is my controller Index code:

public ActionResult Index()
    {
        HELPer.sessionVars sVars = new HELPer.sessionVars
        {
            UnboundFY = "0",
            UnboundCat = "0",
            UnboundFYbetween = "",
            UnboundCHelp = "",
            UnboundN = "",
            UnboundStrUser = "",
            UnboundForm = "",
            UnboundPer = "",
            strID = "",
            UnboundStrUserDataWHERE = "",
            UnboundNClass = "",
            UnboundstrSQL = "",
            strUser = "",
            strTname = "",
            theType = "",
            userCategory = "",
            AdminMode = false,
            AddPresenter = false,
            Mylasses = false,
            Newnt = false,
            Beduled = false,
            EvalMode = false,
            cat = false,
            SCHEDULE = false,
            Travelicy = false
        };

        Session["sessionVars"] = sVars;

        return getMainData();
    }

And this is my Model that code above is calling:

namespace ETTData.Models
{
    public class HELPer
    {
        public class sessionVars
        {
            public string UnboundFY { get; set; }
            public string UnboundCat { get; set; }
            public string UnboundFYbetween { get; set; }
            public string UnboundCHelp { get; set; }
            public string UnboundN { get; set; }
            public string UnboundStrUser { get; set; }
            public string UnboundForm { get; set; }
            public string UnboundPer { get; set; }
            public string strID { get; set; }
            public string UnboundStrUserDataWHERE { get; set; }
            public string UnboundNClass { get; set; }
            public string UnboundstrSQL { get; set; }
            public string strUser { get; set; }
            public string strTname { get; set; }
            public string theType { get; set; }
            public string userCategory { get; set; }
            public bool AdminMode { get; set; }
            public bool AddPresenter { get; set; }
            public bool Mylasses { get; set; }
            public bool Newt { get; set; }
            public bool Beduled { get; set; }
            public bool EvalMode { get; set; }
            public bool cat { get; set; }
            public bool SCHEDULE { get; set; }
            public bool Travelicy { get; set; }
        }
    }
}

So once the view is loaded I check to make sure sessionVars has values and it does.

Now that's all great but what I really need to do is access one of those object values from the Razor view page.

As an example I have tried the following:

 @Session["sessionVars"].AdminMode;

 @model ETTData.Models.HELPer.sessionVars; 

 @Session["sessionVars"].AdminMode.toString();

 @Session["sessionVars"]["AdminMode"];

 @Session["sessionVars"].toString();

 System.Web.HttpContext.Current.Session["sessionVars"] = sVars; //(in controller)
 @Session["sessionVars"].AdminMode; //In Razor View page

All examples above failed for one reason or another.

So what am I doing incorrectly?

Update 1

enter image description here

StealthRT
  • 10,108
  • 40
  • 183
  • 342

3 Answers3

0

You need to cast @Session["sessionVars"] to HELPer.sessionVars:

(HELPer.sessionVars)Session["sessionVars"]

You can then access the properties.

Kevin Raffay
  • 842
  • 5
  • 18
  • That just seems to write out **(HELPer.sessionVars)Session["sessionVars"]** onto the page itself. – StealthRT May 16 '17 at 18:48
  • Have your tried @((HELPer.sessionVars)Session["sessionVars"]).AdminMode ? – Kevin Raffay May 16 '17 at 18:52
  • Doing that causes an error of **Error CS0246 The type or namespace name 'HELPer' could not be found (are you missing a using directive or an assembly reference?)** – StealthRT May 16 '17 at 18:54
  • You need to add a @using to the view with the namespace of your HELPer object (ie. http://stackoverflow.com/questions/3239006/how-do-i-import-a-namespace-in-razor-view-page) – Kevin Raffay May 16 '17 at 20:39
0

So I found it:

I have to do the following on the Razor page:

 @{ ETTData.Models.HELPer.sessionVars sVars = (ETTData.Models.HELPer.sessionVars)HttpContext.Current.Session["sessionVars"]; }

And then I can use:

 @user.AdminMode;
StealthRT
  • 10,108
  • 40
  • 183
  • 342
0

The first thing is you no longer need ...

Session["sessionVars"] = sVars

Next, your controller needs to pass the view-model to the view.

return View(sVars);

Next in your razor you need to specify the type your view model is...

@model ETTData.Models.HELPer

Finally to place a value inside of some markup in Razor, you would do something like this...

<p>Unbound Cat:</p> <b>@model.UnboundCat</b>
MBurnham
  • 381
  • 1
  • 9