将登录逻辑放在部分视图中

时间:2014-01-30 07:56:32

标签: asp.net .net asp.net-mvc c#-4.0 linq-to-sql

我几乎在MVC中开发了一个考勤应用程序,linq到sql,作为一个新手。现在卡在一个地方,LOGGED IN部分,当特定用户的会话开始时,右上角出现LOGOUT按钮,那没关系但是我想做什么我这样得到一个id(员工ID)当前存储在会话中在右上角显示

EMPLOYEE 002 has Logged in,
[LOG OUT]

enter image description here

LogOut实际上是一个部分视图正在呈现我,我已经把我的逻辑用于会话检查。

@if(Session["Employee"] != null) {
    @*<text>Welcome <strong>@User.Identity.Name</strong>!*@
   <text> [ @Html.ActionLink("Log Out", "LogOff", "Account") ]</text>
}
else {
    @:[ @Html.ActionLink("Log In", "LogOn", "Account") ]
}

退出控制器

public ActionResult LogOff()
        {
            Session.Abandon();

            return RedirectToAction("Index", "Home");
        }

1 个答案:

答案 0 :(得分:0)

答案

@if(Session["Employee"] != null) {
    int empId = 0;
    int.TryParse((string)Session["Employee"], out empId);
    <text> <strong>@empId </strong> has logged In <br />
    [ @Html.ActionLink("Log Out", "LogOff", "Account") ]</text>
}
else {
    @:[ @Html.ActionLink("Log In", "LogOn", "Account") ]
}