登录后获取用户数据 [英] Get users data after login
问题描述
我有一个视图Register和webpages_UserProfile表,用于在用户注册后存储数据。然后,用户将被重定向到另一个视图RegisterQue。 Que和UserProfile之间存在外键关系。注入时间是针对用户,然后他或她可以使用视图登录。一旦用户登录,他将被重定向到索引视图,显示他所做的所有que注册。如何在登录后只在索引视图中显示他的que注册?
我在索引视图上使用linq查询。目前它显示索引视图上所有用户的所有que注册
I have a view Register and the webpages_UserProfile table that stores the data after a user signs up. The user will then be redirected to another view RegisterQue. A foreign key relationship exists between Que and UserProfile. Incase time is against a user he or she can then use a view login. Once the user logs in he will be redirected to an index view showing all the que registrations that he made. How do I show only he's que registrations on the index view after he logs in??
I am using a linq query on the index view. Currently it shows all the que registrations for all user on the index view
Public actionresult Index()
{
Using(var db= new dbEntities())
{
return view(db.Ques.ToList());
}
}
推荐答案
您应该更改 IEnumerable<<< Entity>的视图返回类型> 因为此类型负责加载类型集合,即现在在索引视图中加载的类型。如果你想加载多个集合你应该创建一个新的ViewModel类并添加两个属性如下
You should change view return type of IEnumerable<<Entity>> because this type is responsible for loading a collection of type,ie is the type now loading in the index view. if you want to load multiple collection you should create a new ViewModel class and add two property like below
public class HomeViewModel
{
public HomeViewModel()
{
Collection1=new List<yourfirstentity>();
Collection2=new List<loginuserdetails>();
}
public List<yourfirstentity> Collection1{get;set;}
public List<loginuserdetails> Collection2{get;set;}
}
}</loginuserdetails></yourfirstentity></loginuserdetails></yourfirstentity>
从视图中你可以迭代你的两个集合如下
From the view you can iterate your both collection like below
@model HomeViewModel
{
}
//iterate first collection
@foreach (var item in Model.Collection1) {
//blah..blah
}
//iterate second collection
@foreach (var item in Model.Collection2) {
//blah..blah
}
我建议你创建一个每个页面的viewmodel。 Viewmodel只包含包含特定视图所需的所有数据的类
希望这有助于
I recommend you create a viewmodel for every page. Viewmodel nothing but the class which contains all the data needed for a particular view
Hope this helps
这篇关于登录后获取用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!