Display Data

Topics: Writing modules
Nov 24, 2011 at 3:14 PM

Hello, i have a Problem: I have created a new Module and there is save different Versions.

On one Page I want to show the latest Version with a Widget. So now my Problem: how can I create a Widget without insert new Data??

And is it possible to set the title of each Version automatically? (The Title should contain information from the textfield of the Version)


Hope somebody can help me

Nov 24, 2011 at 3:38 PM

Please provide a detailed description of your problem and any code you've written already to help us understand your scenario.

Nov 24, 2011 at 3:46 PM

This is ma Migrations.cs file:

 public class Migrations : DataMigrationImpl {
        public int Create() { // Creating table VersionPartRecord SchemaBuilder.CreateTable("VersionPartRecord", table => table .ContentPartRecord() .Column("Versionsnr", DbType.String) .Column("Datum", DbType.String) .Column("Gesetzesstand", DbType.String) .Column("Datenbankversion", DbType.String) .Column("Kurzbeschreibung", DbType.String) );

            return 1;        } public int UpdateFrom1() {  ContentDefinitionManager.AlterPartDefinition("VersionPart", builder => builder.Attachable());  return 2; } public int UpdateFrom2() {  ContentDefinitionManager.AlterTypeDefinition("Version", cfg => cfg .WithPart("CommonPart")      .WithPart("Containable") .WithPart("BodyPart") .WithPart("VersionPart") .WithPart("LocalizationPart") .Creatable() .Indexed());  return 3; }    public int UpdateFrom3()      {          // Create a new widget content type with our map          ContentDefinitionManager.AlterTypeDefinition("VersionWidget", cfg => cfg             .WithPart("VersionPart")             .WithPart("WidgetPart")             .WithPart("CommonPart")             .WithSetting("Stereotype", "Widget"));             return 4;      }

Than i also wrote an Service:

amespace EPVersionen.Services{   public interface IVersionService : IDependency {        VersionViewModel GetVersionViewModel();   }   public class VersionService   {     private readonly ISessionLocator _sessionLocator;     private readonly IWorkContextAccessor _wca;
     public VersionService(ISessionLocator sessionLocator, IWorkContextAccessor wca)     {       _sessionLocator = sessionLocator;       _wca = wca;     }
     public VersionViewModel GetVersionViewModel()     {       var model = new VersionViewModel();       using (var session = _sessionLocator.For(typeof(VersionPartRecord)))       {         var result = from m in session.Linq<VersionPartRecord>()                      orderby m.Versionsnr                      select m;
          foreach (var item in result)          {            model.Versions.Add(item);          }       }       return model;     }   }



class VersionController : Controller  {    private readonly IVersionService _VersionService;
        public Localizer T { get; set; }        public IOrchardServices Services { get; set; }
        public VersionController(          IVersionService VersionService,            IOrchardServices services) {              _VersionService = VersionService;            Services = services;            T = NullLocalizer.Instance;        }        public ActionResult Versionenansicht()        {
          var model = _VersionService.GetVersionViewModel();                   return View(model);        }  }



So this Widget should be displayed

 @model EPVersionen.Models.VersionPartRecord@{  string Title = "Version " + @Model.Versionsnr + " vom " + @Model.Datum + ", Gesetzesstand: " + @Model.Gesetzesstand; }  <h1><a href="~/ep-lohn/Support/versionen/@Model.Versionsnr">@Title</a></h1>


But when i create a Widget Orchard wants to insert new Data.

So is it possible to 

*) Override the View of the Container Widget just for one Layer or would it be better to

*) use an own Widget, which selects the latest Version by Versionnumber?

If it is possible how can I do that?

Nov 24, 2011 at 4:02 PM

Ok, it's a bit hard to follow with the formatting like that, but I think I get the idea. You perhaps need to rethink some core Orchard concepts:

* A Widget is a special type of content that can be displayed in different zones of the layout. Widgets are added to the page by using layers. You should never be using a Controller to attempt to display a view of a widget.

* All content items (widgets, pages, or anything defined as a content type) need to be displayed by using ContentManager methods. What you have done is pulled out a *single record* of the whole content item (that record is VersionPartRecord) 

* It looks like all you want to do is have a table, not a full content item. So you don't need to use content management. Just create your own "VersionRecord" model, don't inherit from ContentPartRecord. Then use an IRepository<VersionRecord> to access the data, and you can display whatever MVC views you like from it (CRUD, etc.) I'm not sure why you needed to use ISessionLocator in the first place.

Nov 24, 2011 at 4:09 PM

Sorry for the formatting, I am new.

So i can just display it in an Alternate View?
Because my problem was that I use shape tracing to create an Alternate view, but there I had no Data access.

So i tried it the way i posted before.

Nov 24, 2011 at 4:18 PM
Edited Nov 24, 2011 at 4:19 PM

You don't need an Alternate at all. That's only if you want to change an existing template in specific circumstances.

I'm still not sure what you're trying to do, which one of these is it:

- Display a widget in a sidebar or similar zone; that widget holds some custom data

- Or, display a page showing your custom data on its own?

Nov 24, 2011 at 4:22 PM

It looks like this: http://bdata.at/EPLohnInfo,Update.aspx

And I want to Display the Download Sektion with the existing data from the database => it should display automatically the latest version number

So i want to display the data in a widget

I hope it's now clear to you.


Is it also possible to set the Title of a Content Item automatically?

Nov 24, 2011 at 4:31 PM

"Set the title of a content item automatically" - set it automatically from what? You can certainly go content.As<WidgetPart>().Title = "foo". But if you just want to show the title of your download, you should just include that in your view, and hide the normal title (by overriding the template or using Placement).

Can I ask: have you worked through all the tutorials on orchardproject.net? i.e. creating a content part, creating a widget, etc. It would help if you understand how to a) create a widget with a custom part, b) create a view for that widget, c) pass data from your driver into the widget. Then it doesn't matter where you get the data from, the Widget simply tells orchard "display these views in this area".

Nov 24, 2011 at 4:58 PM

Yes I worked trough all the tutorials, but in this point I get one's wires crossed.

I want to display the Title that way: 

string Title = "Version " + @Model.Versionsnr + " vom " + @Model.Datum + ", Gesetzesstand: " + @Model.Gesetzesstand; with the Data i saved in VersionPartRecord.


How can I tell a Widget to display the View Version.cshtml? 

Nov 24, 2011 at 5:27 PM

If you've worked through the tutorials then you should know that you create a ContentPartDriver to emit a view from the widget, right?

Nov 27, 2011 at 10:47 AM

So now I just created an Shape: 

Code of Shape: 
@{  string Title = "Version " + @Model.Versionsnr + " vom " + @Model.Datum + ", Gesetzesstand: " + @Model.Gesetzesstand; }  

<a href="/ep-lohn/Support/versionen/@Model.Versionsnr">@Title</a>


But I don't geht the Data of the Database, So it looks like this "Version vom , Gesetzesstand:"


May you have a hint for me