Reputation: 1558
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.
Upvotes: 0
Views: 342
Reputation: 12630
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"));
Upvotes: 1