2
votes

I am an Orchard CMS beginner and I do not understand how to get and display a list of items in a view.

I've read the article Writing a ContentPart but the examples show how to use 2 properties instead of a list:

Driver

    return ContentShape("Parts_Map", () => shapeHelper.Parts_Map(
        Longitude: part.Longitude,
        Latitude: part.Latitude));

View

<img alt="Location" border="1" src="http://maps.google.com/maps/api/staticmap? 
     &zoom=14
     &size=256x256
     &maptype=roadmap
     &markers=color:blue|@Model.Latitude,@Model.Longitude
     &sensor=false" />

I want to use a list of items in a view.

I've also read the article Orchard CMS Custom Widget View but i do not understand how it works, especially the following line:

 var files = (IContentQuery<FilePart>)Model.Files;

Where can i find additional examples?

Is it below correct? I Use IContentManager contentManager for transfer data to view.

Driver:

public class MyModuleWidgetPartDriver : ContentPartDriver {

private readonly IContentManager contentManager;

public MyModuleWidgetPartDriver(IContentManager contentManager)
{
  this.contentManager = contentManager;
}

protected override DriverResult Display(MyModuleWidgetPart part, string displayType, dynamic shapeHelper)
{
  var MyModuleItems = this.contentManager.List<MyModulePart>(MyModulePart.ContentTypeName).ToArray();

  // List of items
  var MyModuleItemsViewModel = MyModuleItems.Select(MyModule => new MyModuleItemsViewModel
  {
    Title = MyModule.Title,
    Html = MyModule.Html
  });

  return ContentShape("Parts_MyModules", () => shapeHelper.Parts_MyModules(
           promo: Json.Encode(MyModuleItemsViewModel)));
}

MyModulePart:

   public class MyModulePart : ContentPart
{
    public const string ContentTypeName = "MyModule";

    public string Title
    {
        get { return this.As<ITitleAspect>().Title; }
    }

    public string Html
    {
        get { return this.As<BodyPart>().Text; }
    }
}

View\Parts\MyModule.cshtml:

@{
    var MyModuleItems = Model.promo;
}
1
Are you trying to list content items?Hazza
I want get all records (my model) from database and push it into view.gen
Can you show what code do you have so far?ViRuSTriNiTy

1 Answers

0
votes

You can set anything you need in the view onto the shape you're creating from your driver. Do make sure that whenever you perform a "heavy" operation such as querying a database, you do so from within the shape factory lambda (the lambda that actually creates the shape). If you do this outside of the lambda, your query will execute even if your shape won't be displayed, which would obviously be inefficient. For example:

Driver

return ContentShape("Parts_Map", () => {
   return shapeHelper.Parts_Map(
      MyList: _someService.GetMyListOfData(); // The MyList property will be added on the fly to the Parts_Map shape you're creating, and will be available in the view, whose model is in fact this Parts_Map shape.
      Longitude: part.Longitude,
      Latitude: part.Latitude);
});

View

<ul>
@foreach(var item in Model.MyList){
   <li>@item.SomeProperty</li>
}
</ul>

Remember: Shapes are dynamic objects to which you can add properties on the fly. When "rendering a shape", what really happens is that Orchard locates the appropriate Razor view based on metadata stored with the shape (each shape has a Metadata property), and sets the Model of that view to the shape object.