Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orchard CMS: Right way to get field value from view?

Tags:

orchardcms

I have ContainerWidget and custom container type with ShowAllLinkCaption field. Now I have only one solution, and it's looks ugly. What is the right way to get this field value on a Container Widget View?

@*Latest news widget*@
@using Orchard.ContentManagement
@using Orchard.Utility.Extensions
@{
    var contentId = (int)Model.ContentItem.ContainerWidgetPart.Record.ContainerId;
    IContentManager contentManager = WorkContext.Resolve<IContentManager>();    
    var customListContentItem = contentManager.Get(contentId);
    var showAllLinkCaptionField = customListContentItem.Parts.SelectMany(p => p.Fields).First(f => f.Name == "ShowAllLinkCaption");
    var showAllLinkCaptionText = showAllLinkCaptionField.Storage.Get<string>(null);   
}
@Display(Model.Content)
@Html.Link(showAllLinkCaptionText, Url.ItemDisplayUrl(customListContentItem))
like image 798
Alexey Ryazhskikh Avatar asked May 24 '12 11:05

Alexey Ryazhskikh


1 Answers

ContentItem is a dynamic object that allows direct access to parts and fields without having to use those ugly Lambdas. You just need to know the name of the part that has the field, and you can do:

someContentItem.ThePartThatHasTheField.TheField.TheNameOfThePropertyYouWantToAccess
like image 115
Bertrand Le Roy Avatar answered Nov 11 '22 03:11

Bertrand Le Roy