如何返回所有类别的所有条目? [英] How to return all the entries from all categories?

查看:98
本文介绍了如何返回所有类别的所有条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在我的默认,索引页面上返回数据库中的所有条目。它们按类别划分,我将根据其类别返回条目。但如何归还所有呢?



按类别返回条目的Index方法:



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();
      }

推荐答案

如果数据库中有外部密钥,

当你创建模型时,你应该(在你的Category类中)一个虚拟列表到条目



然后你的代码就像那样

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
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

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


这篇关于如何返回所有类别的所有条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆