I am working on displaying data based on the user logged in. In this application, I have added couple of contact information in database. Before, I was able to display all data. Now the thing is I have implemented PagedList in table and while returning data I am not able to return the data of the user who is logged in. All contacts have appeared. Here is my code.
public ActionResult Index(ContactModel contact, string sortOrder, string CurrentSort, int? page)
{
var currentUserId = User.Identity.GetUserId();
contact.UserId = currentUserId;
int pageSize = 5;
int pageIndex = 1;
pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
ViewBag.CurrentSort = sortOrder;
sortOrder = String.IsNullOrEmpty(sortOrder) ? "ContactID" : sortOrder;
IPagedList<ContactModel> cnt = null;
switch (sortOrder)
{
case "ContactID":
if (sortOrder.Equals(CurrentSort))
cnt = db.Contact.OrderByDescending
(m => m.ContactID).ToPagedList(pageIndex, pageSize);
else
cnt = db.Contact.OrderBy
(m => m.ContactID).ToPagedList(pageIndex, pageSize);
break;
case "Contact_Name":
if (sortOrder.Equals(CurrentSort))
cnt = db.Contact.OrderByDescending
(m => m.Name).ToPagedList(pageIndex, pageSize);
else
cnt = db.Contact.OrderBy
(m => m.Name).ToPagedList(pageIndex, pageSize);
break;
case "Email":
if (sortOrder.Equals(CurrentSort))
cnt = db.Contact.OrderByDescending
(m => m.Email).ToPagedList(pageIndex, pageSize);
else
cnt = db.Contact.OrderBy
(m => m.Email).ToPagedList(pageIndex, pageSize);
break;
case "Default":
cnt = db.Contact.OrderBy
(m => m.ContactID).ToPagedList(pageIndex, pageSize);
break;
}
var contactData = db.Contact.Where(x => x.UserId == currentUserId);
return View(cnt);
}
How can I achieve the data of user who logged in with PagedList? Help would be appreciated.
In your code cnt
is only ordered list but where clause is applied on contactData
. You should apply where on cnt
and the return contactData
. Like as following
var contactData = cnt.Where(x => x.UserId == currentUserId);
return View(contactData);
This should solve your issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.