MVC4 LoginPartial(附加信息)

时间:2013-10-31 09:45:21

标签: asp.net asp.net-mvc asp.net-mvc-4 asp.net-membership login-page

我正在使用MVC4和SimpleMembership。一切正常,但我对_LoginPartial视图有疑问。

这是局部视图的代码:

@if (Request.IsAuthenticated) {
    <text>
        Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
        @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
            @Html.AntiForgeryToken()
            <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
        }
    </text>
} else {
    <ul>
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

如何在“Hello,user”旁边显示个人资料图片?我有一个用户模型,其中包含图片的文件路径。这也保存在数据库中。

问题是我不知道如何以正确的方式显示它。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用RenderAction返回包含图片代码的PartialView

    Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })! 
@{ Html.RenderAction("DisplayPhoto", "Account"); }

<强>控制器

public ActionResult DisplayPhoto()
{
   // query the user photo then return the view 
   ViewBag.FilePath = GetUserPhoto(username);
   return PartialView("_DisplayPhoto");
}

<强> PartialView

<img src="@ViewBag.FilePath" alt="photo" />