0
votes

I have some experience with ASP.NET MVC, but since a couple of weeks I am working on a module for Orchard.

What I can't understand is that you are almost mandatored to use Content Parts or Content Types in Orchard, while I just want to get my data from the database en show it my way, not in some article.

Why is this? Is there some way to build a module the way you should do it in ASP.NET MVC, without using all the Orchard stuff? I have been looking at several tutorials, but they are all using parts and drivers and so on.

EDIT:

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

'Hello from view' is visible, so I really get to my view.

1
You really aren't "mandatored" to use content in Orchard. You can create a controller and code away quite happily as if you were working on any other ASP MVC project, but I'd question your use of a framework for creating content driven websites if you don't actually want to use any of the content management features.mdm
We have a big (local) application where a company registers all their stock of machines. these machines are being uploaded every night to the db of the website and this way the website is being updated. So because the user will never touch anything on the website, but will change their stock locally, I don't need to edit an create the records in the website, I only have to display it. We wanna use Orchard to build a nice looking website with some content like news, etc around it, thats why I want to make a module for Orchard, but not with all these features, which are useless for me.avb
Can you please explain me how I can get my data from the database then or refer to a tutorial where this is done without all the Orchard stuff? Thanks for your answeravb
Well you've got a couple of options depending on how the data is accessed. Where/what is the stock database? Is it the Orchard database or a different one? SQL Server, MySQL?mdm
It's in the Orchard database, I just made some tables with Orchard's Migrations feature. I am using SQL Server. Thanks for your help so far, appreciate that.avb

1 Answers

1
votes

Assuming all your records have been created via Orchard migrations, then you can use the IRepository<> interface to access your data.

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

Views/Record/Index.cshtml:

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

I've had problems in the past with primary keys not being set up correctly from my migrations (it's not enough just to use .PrimaryKey()), I use something based on the following to create non-content records:

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