在剃刀视图中访问已定义的会话/变量

时间:2015-10-06 03:23:16

标签: asp.net-mvc razor

这是我的部分视图,我的部分视图正在被重复使用,因此它没有强类型。

@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>

所以从这里开始,我有一个session存储了一个类,我想在我的剃刀视图中检索它,但我不知道如何。

这是我需要它的代码行:

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

如何在链接中访问yearterm变量?如果我直接在我的Url.Action

上输入红线,我会得到红线

1 个答案:

答案 0 :(得分:1)

您可以在year语句范围之外声明termif变量,因此,您可以在页面上访问它们。

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>