[英]Mvc 4 - How to pass parameters to a partial view with viewbag
I have a _Layout page and in this _Layout page, I am calling a Partial View as below.我有一个 _Layout 页面,在这个 _Layout 页面中,我调用了一个 Partial View,如下所示。
<div>
@{Html.RenderAction("_Account", "Home");}
</div>
_Account.cshtml _Account.cshtml
... ...
@if (User.Identity.IsAuthenticated)
{
<p class="navbar-text navbar-right"> ****I want to write here "UserName"****</p> }
As you see, I want to write userName if the user is authenticated.如您所见,如果用户通过身份验证,我想写入 userName。 ( this is my question :) ) (这是我的问题:))
As you can expect, I have an Login controller..正如您所料,我有一个登录控制器..
[HttpPost]
public ActionResult Index(UserModel model, string returnUrl)
{
// My codes for login ...
var response = _userService.UserGet(request.UserName,request.Password);
if(response != null)
{
ViewBag.UserName = response.User.Name;
}
// My another codes....
}
So, here is my question.所以,这是我的问题。 I want to use this "ViewBag.UserName" in _Account partial view.我想在 _Account 部分视图中使用这个“ViewBag.UserName”。 I don t wanna use cookies.我不想用饼干。 Is there any way to to id?有什么办法可以识别吗?
As @Satpal said正如@Satpal 所说
@if (User.Identity.IsAuthenticated)
{
<p class="navbar-text navbar-right">@ViewBag.UserName</p>
}
Isn't it simple???是不是很简单???
You can do it like this:你可以这样做:
Main View:主视图:
@Html.RenderAction("AccountPartial", "Home")
Action:行动:
[HttpPost]
public ActionResult AccountPartial(UserModel model, string returnUrl)
{
// My codes for login ...
var response = _userService.UserGet(request.UserName,request.Password);
if(response != null)
{
ViewBag.UserName = response.User.Name;
}
// My another codes....
return PartialView("~/Views/Home/_Account.cshtml",model);
}
Partial View:部分视图:
@if (User.Identity.IsAuthenticated)
{
<p class="navbar-text navbar-right">@ViewBag.UserName</p>
}
只需使用@User.Identity.Name 来获取用户名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.