0

我正在使用MVC4和SimpleMembership。一切正常,但我有一个关于_LoginPartial视图的问题。MVC4 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> 
} 

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

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

预先感谢您!

回答

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" /> 
+0

所以我可以添加HTM将l.RenderAction添加到_LoginPartial.cshtml文件中,将DisplayPhoto添加到AccountController。对于PartialView:我需要创建一个新的还是可以将该行()添加到_LoginPartial.cshtml视图? – Odrai

+0

您需要为'DisplayPhoto'方法创建一个新的'PartialView'。另外,'AccountController'只是一个例子,如果你愿意,你可以改变它。 – WannaCSharp

+0

如果我创建一个新的PartialView它是空白的,所以我添加了photo它,但它只显示单词“photo”...? – Odrai