MVC 登录问题

w091b 2013-08-26 04:15:16
先上图,做讲解(Home的Index视图)

图中由红框框包围的都写在了_Layout.cshtml页面里,因为每一张页面都会显示
当然“a"和“b”不是同时存在的,这里只是为解说更方便一点,做了如此处理。
当点击登陆后,转到Account控制器里的Login里面
  public ActionResult Login(string UserName,string UserPwd)
{
Account_Login login = new Account_Login();
login.UserName = UserName;
login.UserPsw = UserPwd;
DinnerContext context = new DinnerContext();
var user= context.Custormers.Where(a=>a.UserName==UserName).Where(a=>a.UserPsw ==UserPwd).SingleOrDefault();
if (user == null)
{
return Json("0");
}
else
{
FormsAuthentication.SetAuthCookie(login.UserName, true);
HttpContext.Session["username"] = UserName;
Session["PassWord"] = UserPwd;
return Json("1");
}

}


然后在_Layout.cshtml里面根据获取的“0”或者“1”来判断用户是否登录,来决定显示“a"区还是”b“区,到这里都可以,但是刷新后,执行到Home里面Index方法,执行Index方法后,就得判断用户是否已经登录,然后再隐藏“a”区,显示“b”区
 public ActionResult Index()
{
if (Session["username"] != null && Session["PassWord"] != null)
{
???这里面我不知道该怎么写,应为登录的部分在_Layout.cshtml里面
}
return View();
}

...全文
237 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
w091b 2013-08-28
  • 打赏
  • 举报
回复
引用 4 楼 ccyb101 的回复:
大哥。你绕圈子了,用ajax 试试 !
用的就是Ajax
ccyb101 2013-08-28
  • 打赏
  • 举报
回复
大哥。你绕圈子了,用ajax 试试 !
w091b 2013-08-28
  • 打赏
  • 举报
回复
问题补充:因为“b"区欢迎您后面还得显示当前的用户名,所以应该要返回model什么的,请高手指点?
w091b 2013-08-26
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
一样的,这里传一个参数,在View中再传给局部视图。
不太懂版主的意思,能讲解详细些嘛
threenewbee 2013-08-26
  • 打赏
  • 举报
回复
一样的,这里传一个参数,在View中再传给局部视图。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧