C# ASP.NET:显示模型中特定范围的项

C# ASP.NET:显示模型中特定范围的项,c#,asp.net,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc 5,我正在为一个朋友建立一个网站。我还不是很好,但我正在学习。这就是我面临的问题 public ActionResult CategoryRetail() { var allCategories = m_db.Categories; return View(allCategories); } 在视图中,我使用它来显示模型中的所有类别 <ul> @foreach (var category in Model)

我正在为一个朋友建立一个网站。我还不是很好,但我正在学习。这就是我面临的问题

 public ActionResult CategoryRetail()
    {
        var allCategories = m_db.Categories;
        return View(allCategories);
    }
在视图中,我使用它来显示模型中的所有类别

   <ul>
        @foreach (var category in Model)
        {
          <li>@category.CategoryName</li>
        }
   </ul>
问题是,如何根据ID显示模型中特定范围的类别? 我想让一个部分显示7个类别,然后另一个部分显示另一组,依此类推。
想要一些可以实现这一点的示例。

您不应该简单地将DB对象传递到视图中,而应该创建一个通常称为ViewModel的中间对象来保存视图所需的不同数据集

因此,ViewModel将包含两个类别列表,一个由一个ID过滤,另一个由另一个ID过滤

然后,视图可以访问一个UI元素的Model.FirstSetOfCategories,以及另一个UI元素的Model.SecondSetOfCategories,依此类推

希望这有意义


有关ViewModel的详细说明,请参见。

可以使用Linq Lambda表达式,其中

看法

您可以创建一个截面表:

Id  | SectionName
------------------
 1  | Header
 2  | LeftNav
 3  | Footer
最后,创建CategorySection表:

Id  | CategoryId | SectionId
----------------------------
 1  |     1      |     3      <-- Maps Books to Footer
 2  |     2      |     1      <-- Maps Music to Header, etc etc

在不知道对象的情况下,我假设您可以执行m_db.Categories.Take7。。。。但我认为你应该从你掌握的一点点信息中重新思考你的设计given@TheGeekYouNeedTake可以跳过,但是Take不是更容易吗?谢谢你们的回答,伙计们!我当时正在搬家,开始攻读硕士学位,所以没有时间回来。我还有一些问题。填充IEnumerable的好方法是什么?什么是一个好的和有效的方式来安排它。是一个需要某种类型集合的接口。对于IEnumerable,大多数数据库查询都可以正常工作。我已经更新了我的答案。
@model MyProject.Models.MyModel

<ul>
        @foreach (var category in Model.Cat2)
        {
          <li>@category.CategoryName</li>
        }
</ul>
public class CategoryModel
{
    public IEnumerable<Category> Section1Categories{ get; set; }
    public IEnumerable<Category> Section2Categories{ get; set; }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
    } // end class category
} // end class CategoryModel
public Models.CategoryModel GetAllCategories()
{
    var TheModel = new Models.CategoryModel();
    TheModel.Section1Categories = GetCategoryRange(1, 7);
    TheModel.Section2Categories = GetCategoryRange(7, 9);
} // end Get All Categories

private IEnumerable<Models.CategoryModel.Category> GetCategoryRange(Int32 LowId, Int32 HighId)
{
    // This is using LINQ.  You could accomplish this with other technologies as well
    var cats = (from c in m_db.Category
                where c.Id >= LowId &&
                c.Id <= HighVal
                select new Models.CategoryModel.Category()
                {
                    Id = c.Id,
                    Name = c.Name
                });
    return cats;
} // end GetCategoryRange
public ActionResult MyPage()
{
    BLL.CategoryMethods cm = new BLL.CategoryMethods();
    Models.CategoryModel TheModel = cm.GetAllCategories();
    return View(TheModel);
} // end MyPage
<ul id="CategorySection1">
  @foreach (var cat in Model.Section1Categories)
  {
    <li data-catid="@cat.Id">@cat.Name</li>
  }
<ul>
<ul id="CategorySection2">
  @foreach (var cat in Model.Section2Categories)
  {
    <li data-catid="@cat.Id">@cat.Name</li>
  }
<ul>
Id  | CategoryName
-----------  
 1  | Books  
 2  | Music  
 3  | Sports
Id  | SectionName
------------------
 1  | Header
 2  | LeftNav
 3  | Footer
Id  | CategoryId | SectionId
----------------------------
 1  |     1      |     3      <-- Maps Books to Footer
 2  |     2      |     1      <-- Maps Music to Header, etc etc
var footerCategories = (from c in m_db.Category
                        join cs in m_db.CategorySections on c.Id equals cs.CategoryId
                        where cs.SectionId == 3
                        select new Models.CategoryModel.Category()
                        {
                           Id = c.Id,
                           Name = c.Name
                        });