code_junkie

Passing arguments to views in ASP.NET MVC

Nov 13th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. public bool Uncomplete()
  2. {
  3. bool AnyLeft = false;
  4. var tasks = from t in db.Tasks orderby t.EntryDate descending select t;
  5. foreach (Task match in tasks)
  6. {
  7. if (match.IsCompleted == false)
  8. {
  9. AnyLeft = true;
  10. }
  11. }
  12.  
  13. return AnyLeft;
  14. }
  15.  
  16. public ActionResult Index()
  17. {
  18. bool AnyLeft = Uncomplete();
  19. var tasks = from t in db.Tasks orderby t.EntryDate descending select t;
  20. return View(tasks.ToList());
  21. }
  22.  
  23. <% if (AnyLeft == false)
  24. { %>
  25. <h1 class="green">My Tasks</h1>
  26. <% }
  27. else
  28. { %>
  29. <h1 class="red">My Tasks</h1>
  30. <% } %>
  31.  
  32. return View(tasks.ToList(), AnyLeft);
  33.  
  34. public ActionResult Index()
  35. {
  36. ViewData["AnyLeft"] = Uncomplete();
  37. var tasks = from t in db.Tasks orderby t.EntryDate descending select t;
  38. return View(tasks.ToList());
  39. }
  40.  
  41. <% if (!(bool)ViewData["AnyLeft"])
  42. { %>
  43. <h1 class="green">My Tasks</h1>
  44. <% }
  45. else
  46. { %>
  47. <h1 class="red">My Tasks</h1>
  48. <% } %>
  49.  
  50. public static class TaskExtensions {
  51. public static bool Uncomplete(this IEnumerable<Task> tasks) {
  52. return (from t in tasks where !t.IsCompleted select t).Any();
  53. }
  54. }
  55.  
  56. <h1 class="<%= (tasks.Uncomplete()) ? "Red" : "Green" %>">My Tasks</h1>
  57.  
  58. public bool Uncomplete()
  59. {
  60. bool AnyLeft = false;
  61.  
  62. var tasks = from t in db.Tasks
  63. where !t.IsCompleted // or you could use t.IsCompleted == false
  64. orderby t.EntryDate descending select t;
  65.  
  66. AnyLeft = tasks.Any;
  67.  
  68. return AnyLeft;
  69. }
  70.  
  71. foreach (Task match in tasks)
  72. {
  73. if (!match.IsCompleted)
  74. {
  75. AnyLeft = true;
  76. break;
  77. }
  78. }
Add Comment
Please, Sign In to add comment