Advertisement
LoganYoung87

Untitled

Apr 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. // /Home/Index.cshtml contains a link to /Title/<titleId>
  2. // Clicking on the link gives an HTTP 404 error
  3. // URL: http://localhost:50359/Title/1/
  4.  
  5. public class TitleController : BaseController
  6. {
  7.     // GET: Title
  8.     public ActionResult Index(int TitleId)
  9.     {
  10.         // load the DbContext
  11.         ForumEntities Db = new ForumEntities();
  12.  
  13.         // get a list of Threads for the selected Title
  14.         List<Thread> DbThreads = new List<Thread>();
  15.         DbThreads = Db.Threads.Where(x => x.Id == TitleId && x.Deleted == false).ToList();
  16.  
  17.         // build a ViewModel for each thread and add it to a list
  18.         List<ThreadViewModel> Threads = new List<ThreadViewModel>();
  19.         foreach (Thread Thread in DbThreads)
  20.         {
  21.             Threads.Add(new ThreadViewModel
  22.             {
  23.                 Author = Db.Users.FirstOrDefault(x => x.Id == Thread.UserId).Username,
  24.                 Id = Thread.Id,
  25.                 Date = Thread.CreatedOn,
  26.                 Name = Thread.Name,
  27.                 PostCount = Db.Posts.Where(x => x.ThreadId == Thread.Id && x.Deleted == false).Count()
  28.             });
  29.         }
  30.  
  31.         // build the ViewModel for this particular view which contains the requested threads
  32.         ListThreadsViewModel ThreadList = new ListThreadsViewModel
  33.         {
  34.             TitleName = Db.Titles.FirstOrDefault(x => x.Id == TitleId).Name,
  35.             Threads = Threads
  36.         };
  37.  
  38.         return View(ThreadList);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement