为什么在Orchard中使用ContentParts?

时间:2012-11-15 09:50:28

标签: asp.net-mvc orchardcms

我有一些ASP.NET MVC的经验,但是几周后我正在为Orchard开发一个模块。

我无法理解的是,你几乎被要求在Orchard中使用内容部分或内容类型,而我只想从数据库中获取数据并按照我的方式显示,而不是在某篇文章中。

这是为什么?是否有一些方法可以像在ASP.NET MVC中那样构建模块,而不使用所有Orchard的东西?我一直在看几个教程,但他们都在使用零件和驱动程序等等。

编辑:

Machine.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PowerAll.Voorraad.Models
{
  public class MachineRecord
  {
    public int Id { get; set; }
    public int MachineNumber { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public char PriceType { get; set; }
    public decimal Price { get; set; }
    public int Year { get; set; }
  }
}

Migrations.cs

namespace PowerAll.Voorraad
{
  public class Migrations : DataMigrationImpl
  {
    public int Create()
    {
      SchemaBuilder.CreateTable("MachineRecord", table => table
        .Column<int>("Id", column => column.PrimaryKey().Identity())
        .Column<int>("MachineNumber", column => column.NotNull())
        .Column<string>("Title", column => column.NotNull().WithLength(40))
        .Column<string>("Description", column => column.WithLength(70))
        .Column<char>("PriceType", column => column.NotNull().WithLength(1))
        .Column<decimal>("Price", column => column.NotNull())
        .Column<int>("Year", column => column.WithLength(4))
      );

      // Return the version that this feature will be after this method completes
      return 1;
    }
  }
}

MachineController.cs

namespace PowerAll.Voorraad.Controllers
{
  [Themed]
  public class MachineController : Controller
  {
    private readonly IRepository<MachineRecord> machineRecords;

    public MachineController(IRepository<MachineRecord> MachineRecords) {
        machineRecords = MachineRecords;
    }

    public ActionResult Index() {
        // Here we're just grabbing records based on some fictional "Deleted" flag
        var items = machineRecords.Table;
        // Items is now an IEnumerable<MachineRecord>

        return View(items);
    }
  }
}

Index.cshtml

@model IEnumerable<PowerAll.Voorraad.Models.MachineRecord>

<ul>
    @foreach(var item in Model) {
        <li>@item.Id</li>
        <li>@item.MachineNumber</li>
        <li>@item.Title</li>
        <li>@item.Description</li>
        <li>@item.PriceType</li>
        <li>@item.Price</li>
        <li>@item.Year</li>
    }
</ul>
Hello from view

'来自视图的'是可见的,所以我真的看到了我的观点。

1 个答案:

答案 0 :(得分:1)

假设您的所有记录都是通过Orchard迁移创建的,那么您可以使用IRepository<>界面访问您的数据。

E.g。

RecordController.cs:

public class RecordController : Controller {
    private readonly IRepository<CustomRecord> _customRecords;

    public RecordController(IRepository<CustomRecord> customRecords) {
        _customRecord = customRecords;
    }

    public ActionResult Index() {
        // Here we're just grabbing records based on some fictional "Deleted" flag
        var items = _customRecords.Fetch(r => r.Deleted == false);
        // Items is now an IEnumerable<CustomRecord>

        return View(items);
    }
}

查看/记录/ Index.cshtml:

@model IEnumerable<CustomRecord>
<ul>
    @foreach(var item in Model) {
        <li>@item.Name</li>
    }
</ul>

过去我遇到过一些问题,我的迁移中没有正确设置主键(仅仅使用.PrimaryKey()是不够的),我使用基于以下内容的东西来创建非内容记录:< / p>

SchemaBuilder.CreateTable(typeof(CountryRecord).Name,
                                  table => table.Column<int>("Id", c => c.PrimaryKey().Identity())
                                                .Column<string>("Code")
                                                .Column<string>("Name"));