Reputation: 6090
I am having a couple of issues on my mvc page. When a user logs in they should see the posts that they have created however at the moment users are seeing all of the posts by all users. (Linq issue from my index page)
Also BlogUser is displaying the entire contents of that table. I only want to display the email address. I tried BlogUser.Email
in my Index but it threw an error.
The results I am getting just now look like this:
![enter image description here][1]
View:
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.BlogUser)
</th>
Controller:
public ActionResult Index()
{
Post post = new Post();
post.BlogUserEmail = User.Identity.Name;
var posts = db.Posts.Include(p => p.BlogUser).Include(p => p.Category);
//var posts = db.Posts.Include(p => p.BlogUser.Email).Include(p => p.Category);
return View(posts.ToList());
}
![enter image description here][2]
Upvotes: 0
Views: 116
Reputation: 5771
For the first issue where all your posts are shown, it is because you are not applying a filter to the posts. You need to filter it by the user.
var posts = db.Posts.Where( p => p.BlogUserEmail == User.Identity.Name )
.Include(p => p.BlogUser).Include(p => p.Category);
return View(posts.ToList());
For the second part where all the user details are shown, display the Email property or Name according to your requirement. You are currently passing the entire object.
@Html.LabelFor(model => model.BlogUser.Email)
Upvotes: 2
Reputation: 48279
public ActionResult Index() { Post post = new Post(); post.BlogUserEmail = User.Identity.Name; var posts = db.Posts.Include(p => p.BlogUser).Include(p => p.Category); return View(posts.ToList()); }
You aren't filtering there. And the post
variable is never used.
Should it be
var posts = db.Posts
.Where( p => p.PostUserEmail == User.Identity.Name )
.Include(p => p.BlogUser).Include(p => p.Category);
return View(posts.ToList());
perhaps?
Upvotes: 3
Reputation: 7338
Your could do it like this. Select the logged in User from the Database and select the Users Posts
var posts = db.BlogUser.First(s => s.Email == User.Identity.Name).Select(t => t.Posts).ToList();
Upvotes: 1