Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i want on my Default, Index page to return all the entries that have in database. They are devided by categories, and i am returning the entries depend on their category. But how to return all of then?

The Index method that return entries by category:

public ActionResult Index([Bind(Prefix = "Id")] int categoryId)
      {
          var category = _db.Categories.Find(categoryId);
          if (category != null)
          {
              return View(category);
          }
          return HttpNotFound();
      }
Posted
Comments
Sinisa Hajnal 1-Oct-14 7:50am    
Change the code so it gets all if id = 0 or null or something?
Jameel VM 1-Oct-14 7:57am    
change your code from var category = _db.Categories.Find(categoryId); to var category = _db.Categories.ToList();

1 solution

if you have a external key in the database,
when you created the model you should have (inside your Category class) a virtual list to the Entries

then your code should be like that
C#
public ActionResult Index([Bind(Prefix = "Id")] int categoryId)
      {
          var category = _db.Categories.Find(categoryId);
          if (category != null)
          {
              return View(category.Entries);
          }

          return HttpNotFound();
      }


otherwise you should filter your entries like that

C#
public ActionResult Index([Bind(Prefix = "Id")] int categoryId)
      {
          var category = _db.Categories.Find(categoryId);
          if (category != null)
          {
              var entries = _db.Entries.Where(e=>e.CategoryId == categoryId).ToList();
              return View(entries);
          }

          return HttpNotFound();
      }


i'm not sure if i understood correctly what you need, please tell me if i misunderstood

regards
 
Share this answer
 
Comments
PetarS089 3-Oct-14 7:16am    
Yes, that helped me, Thank you.

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