【发布时间】:2010-10-27 18:47:39
【问题描述】:
以下是由 Visual Studio (LogOnUserControl.ascx) 创建的标准默认 ASP.NET MVC 项目中的 LogOn 用户控件:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
if (Request.IsAuthenticated) {
%>
Welcome <b><%: Page.User.Identity.Name %></b>!
[ <%: Html.ActionLink("Log Off", "LogOff", "Account") %> ]
<%
}
else {
%>
[ <%: Html.ActionLink("Log On", "LogOn", "Account")%> ]
<%
}
%>
插入母版页:
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %>
</div>
<%: Page.User.Identity.Name %> 代码显示当前登录用户的登录名。
如何显示用户的FirstName,保存在个人资料中?
我们可以在控制器中读取它,如下所示:
ViewData["FirstName"] = AccountProfile.CurrentUser.FirstName;
例如,如果我们尝试这样做:
<%: ViewData["FirstName"] %>
它仅在分配了ViewData["FirstName"] 值的控制器调用的页面上呈现。
【问题讨论】:
标签: asp.net-mvc user-controls master-pages