Carlos Miguel Colanta
Carlos Miguel Colanta

Reputation: 2823

Accessing defined session / variables inside razor view

Here is my partial view that is being re-used on some of my views so it's not strongly typed.

@if (Session["time"] != null)
{
    MvcApplication6.Models.calendar cl = (MvcApplication6.Models.calendar)Session["time"];
    var year = cl.schoolyear;
    var term = cl.term;
}



<ul class="lispacer">
    <li><a href="@Url.Action("Details", "Management")">View profile</a></li>
    <li><a href="@Url.Action("Search", "Management")">Search Students</a></li>
    <li><a href="@Url.Action("classes", "Management", new { schoolyear =  , term =  })">Manage Subjects</a></li>
    <li><a href="@Url.Action("calendar", "Management")">Set Calendars</a></li>
    </ul>
    <form id="fooForm" action="@Url.Action("Logout", "Management")" method="post">
        <a href="#" id="submit_link" class="button">Log out</a>
    </form>

So from here, i have a session that stored a class, i want to retrieve it on my razor view but i don't know how.

here is the line of code where i need it:

<li><a href="@Url.Action("classes", "Management", new { schoolyear =  , term =  })">Manage Subjects</a></li>

How do i access year and term variable inside my link? i get red lines if i type them directly on my Url.Action

Upvotes: 0

Views: 746

Answers (1)

You can declare year and term variables out of the if statement scope, so, you can access them on the page.

var year = 0;
var term = ""; 

@if (Session["time"] != null)
{
    MvcApplication6.Models.calendar cl = (MvcApplication6.Models.calendar)Session["time"];
    var year = cl.schoolyear;
    var term = cl.term;
}



<ul class="lispacer">
    <li><a href="@Url.Action("Details", "Management")">View profile</a></li>
    <li><a href="@Url.Action("Search", "Management")">Search Students</a></li>
    <li><a href="@Url.Action("classes", "Management", new { schoolyear =  , term =  })">Manage Subjects</a></li>
    <li><a href="@Url.Action("calendar", "Management")">Set Calendars</a></li>
</ul>
<form id="fooForm" action="@Url.Action("Logout", "Management")" method="post">
    <a href="#" id="submit_link" class="button">Log out</a>
</form>

<li><a href="@Url.Action("classes", "Management", new { schoolyear =  year, term = term })">Manage Subjects</a></li>

Upvotes: 1

Related Questions