仍然试图通过MVC4掌握新的SimpleMembership.我更改了模型,使其包括可以正常工作的姓氏和姓氏.
Still trying to get to grips to the new SimpleMembership with MVC4. I changed the model to include Forename and Surname which works fine.
我想更改登录时显示的信息,而不是在视图中使用User.Identity.Name,我想要做类似User.Identity.Forename的操作,什么是最好的方法?
I want to change the information displayed when logged in so instead of using User.Identity.Name in the View I want to do something like User.Identity.Forename, what's the best way to accomplish this?
推荐答案Yon可以利用ASP.NET MVC中提供的@Html.RenderAction()功能来显示此类信息.
Yon can leverage @Html.RenderAction() feature available in ASP.NET MVC to display this kind of info.
_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") ] }这可以做一些事情,并提供一些选项:
This does a few things and brings in some options:
更多推荐
使用SimpleMembership获取用户信息
发布评论