Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
Public actionresult Index()
{
  Using(var db= new dbEntities())
   {
     return view(db.Ques.ToList());
   }
}
Posted
Updated 6-Sep-13 8:04am
v3
Comments
Jameel VM 5-Sep-13 15:09pm    
what is the pblm now?
ZainNabi 5-Sep-13 15:16pm    
How do I show only the currently logged in user he's data on the index view i.e que registrations
Jameel VM 6-Sep-13 1:36am    
can you post the index view code by improving the question?
ZainNabi 6-Sep-13 10:56am    
@model IEnumerable<qwingsystem.models.que>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutPage2.cshtml";
}

Index



<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Q_Description)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Q_Description)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}

</table>
ZainNabi 6-Sep-13 11:11am    
public ActionResult Index()
{
MembershipUser membershipUser = Membership.GetUser();
int userID = (int)membershipUser.ProviderUserKey;

var query = from x in db.Ques
where x.UserId=userID
select new
{
x.Q_Description
};
return View(query.ToList());
}
These are my model classes

public class Que
{

[Key]
[Display(Name = "Queue ID")]
public int ID { get; set; }

[Display(Name = "Queue Description")]
public string Q_Description { get; set; }

[Key]
[ForeignKey("UserProfile")]
[System.Web.Mvc.HiddenInput(DisplayValue = false)]
public int UserId { get; set; }

}

[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string LastName { get; set; }
[EmailAddress]
public string Email { get; set; }
public string Mobile { get; set; }
public virtual ICollection<que> Que { get; set; }
}

1 solution

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
C#
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
C#
@model HomeViewModel
{
}
//iterate first collection
@foreach (var item in Model.Collection1) {
//blah..blah
}

//iterate second collection
@foreach (var item in Model.Collection2) {
//blah..blah
}


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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900