[英]ASP.NET MVC Controller to View Passing data
I started learning ASP.NET and I am still not completely familiar with it. 我开始学习ASP.NET,但我仍然不太熟悉。 I need to pass Account Controller data to my home index view.
我需要将“帐户控制器”数据传递到我的家庭索引视图。 I am manipulating default authentication.
我正在处理默认身份验证。 I added a new column (Gender) into the ASPNETUSERS table using migration.
我使用迁移将新列(性别)添加到ASPNETUSERS表中。 I need to get the current login user details and show it into my
Index
view of HomeController
. 我需要获取当前的登录用户详细信息,并将其显示在
HomeController
Index
视图中。
I already filtered by using below code inside AccountController
. 我已经通过在
AccountController
使用以下代码进行过滤。
var currentUserId = User.Identity.GetUserId();
var x = UserManager.FindById(currentUserId);
ViewBag.Gender = x.Gender;
I think this code can be filtered particular column for the login userid. 我认为可以将此代码过滤为登录用户ID的特定列。 Am i thinking wrong about these?
我在想这些吗? if i am wrong please explain it.
如果我错了,请解释一下。
The main problem which i am facing right now is to pass this @ViewBag.Gender
to Index
view. 我现在面临的主要问题是将此
@ViewBag.Gender
传递给Index
视图。 Is it possible to use @ViewBag.Gender
in the Index
view of HomeController
? 是否可以在
HomeController
的Index
视图中使用@ViewBag.Gender
? My understanding is the ViewBag
has to be defined inside the HomeController
to pass Index
view page of HomeController
. 我的理解是
ViewBag
有内部定义HomeController
通过Index
视图页HomeController
。
How can I pass AccountController
data to Index
view of HomeController
? 如何将
AccountController
数据传递到HomeController
Index
视图? I am not referring to use ViewBag
. 我不是指使用
ViewBag
。 As long as it passes the value i am happy with that. 只要它传递了价值,我就会对此感到满意。 And please give me some suggestions to do this perfectly.
并请给我一些建议以完美地做到这一点。
I actually found the answer from another question area. 我实际上从另一个问题区域找到了答案。 get current user email in Identity
获取身份中的当前用户电子邮件
ApplicationUser x = System.Web.HttpContext.Current.GetOwinContext()
.GetUserManager<ApplicationUserManager>()
.FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
ViewBag.Gender = x.Gender;
I can use this in my HomeController
and pass the ViewBag
to Index
of the HomeController
using @ViewBag.Gender
我可以在
HomeController
使用它,并使用@ViewBag.Gender
将ViewBag
传递给HomeController
Index
To work this code without any error I have to insert below using references 要运行此代码而没有任何错误,我必须在下面使用引用插入
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using ProjectName.Models;
Thanks for your suggestions 谢谢你的建议
Use TempData["gender"] in AccountController and redirect to HomeController. 在AccountController中使用TempData [“ gender”]并重定向到HomeController。 In HomeController get that data in ViewBag and use it on View.
在HomeController中,在ViewBag中获取该数据,然后在View上使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.