仍然试图通过MVC4来掌握新的SimpleMembership。我将模型更改为包含Forename和Surname,工作正常。
我想更改登录时显示的信息,而不是在View中使用User.Identity.Name我想做像User.Identity.Forename这样的事情,最好的方法是什么?
答案 0 :(得分:5)
Yon可以利用ASP.NET MVC中提供的@Html.RenderAction()
功能来显示此类信息。
_Layout.cshtml查看
@{Html.RenderAction("UserInfo", "Account");}
查看模型
public class UserInfo
{
public bool IsAuthenticated {get;set;}
public string ForeName {get;set;}
}
帐户控制器
public PartialViewResult UserInfo()
{
var model = new UserInfo();
model.IsAutenticated = httpContext.User.Identity.IsAuthenticated;
if(model.IsAuthenticated)
{
// Hit the database and retrieve the Forename
model.ForeName = Database.Users.Single(u => u.UserName == httpContext.User.Identity.UserName).ForeName;
//Return populated ViewModel
return this.PartialView(model);
}
//return the model with IsAuthenticated only
return this.PartialView(model);
}
UserInfo查看
@model UserInfo
@if(Model.IsAuthenticated)
{
<text>Hello, <strong>@Model.ForeName</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]
</text>
}
else
{
@:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}
这做了一些事情并带来了一些选择: