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

I have a ContainerWidget and a custom container type with a ShowAllLinkCaption field. Now I have only one solution, and it looks ugly. What is the correct way to get this field value in the 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))
+5
source share
1 answer

ContentItem is a dynamic object that provides direct access to parts and fields without the need to use these ugly Lambdas. You just need to know the name of the part in which there is a field, and you can do:

someContentItem.ThePartThatHasTheField.TheField.TheNameOfThePropertyYouWantToAccess
+13
source

All Articles