【发布时间】:2012-10-30 11:59:35
【问题描述】:
我使用目录服务登录我的页面。我需要将用户名传递到我的母版页以在所有页面中显示用户名。
我获得了用户名并将其存储在 ViewData 中。如何在 masterpage 中传递 viewdata 值。
我的代码:
[HttpPost]
public ActionResult Index(LoginModels model)
{
if (ModelState.IsValid)
{
string DisplayUserName = string.Empty;
string LoginUser = model.Userid;
string LoginPassword = model.Password;
string name = model.UserName
if (ValidateActiveDirectoryLogin(LoginUser, LoginPassword, out DisplayUserName) == true)
{
model.UserName = DisplayUserName;
ViewData["UserName"] = "Welcome" + DisplayUserName;
return RedirectToAction("Index", "MPP", new { UserID = LoginUser });
}
else
{
ModelState.AddModelError("","Invalid Username or Password");
}
}
return View();
}
在布局页面中:
@{ @ViewData["UserName"] }
我尝试了以下方式来显示用户名。但它会引发 nullexception。
编辑:
@foreach (var m in IEnumerable<SampleECommerce.Models.LoginModels>)ViewData["UserName"])
{
@m.UserName
}
【问题讨论】:
标签: c# asp.net asp.net-mvc-3 master-pages