有兴趣的可以看看菜鸟教程的 http://www.runoob.com/aspnet/razor-cs-loops.html
1.ViewData展示登陆的Session信息
Controller:
public ActionResult Index() { if (Session["username"] == null) { Response.Redirect("/Login/Index"); } else { ViewData["username"] = Session["username"].ToString(); } return View(); }
View:
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <i class="fa fa-user-o fa-fw"></i> @if (ViewData["username"]!=null) { @ViewData["username"].ToString(); } <span class="caret"></span> </a>
效果: