umbraco razor - getting fields from content

[亡魂溺海] 提交于 2019-11-28 08:50:46

There are some great resources you should take a look at while you are learning Razor:

  1. Umbraco Razor Feature Walkthrough - An eight part blog post series of many of the new Razor features in Umbraco 4.7 with examples.

  2. Razor DynamicNode Cheat Sheet - A PDF of all the properties and methods available to the Razor DynamicNode object (that includes @Model).

  3. Cultiv Razor Examples - An Umbraco website that you can download and open with WebMatrix or IIS and see various ways to access properties with Razor.

  4. Razor snippets - A compilation of different snippets, examples, etc. from Our Umbraco.

But in answer to your question, to get a property of a specific node you have to get the actual DynamicNode object first, then use the property alias to access the property value. Example:

@{
    //Get the node
    dynamic node = Library.NodeById(1720);

    // Display the property
    @node.newsIntro
}

To access a property from the current page, you simply use Model:

@Model.newsIntro

or

@Model.bodyText

or

@Model.Name

First, get an IPublishedContent object from the TypedContent method and then use GetPropertyValue to retrieve the value of the field.

@{
  int nodeId = 1720;
  IPublishedContent contentNode = Umbraco.TypedContent(nodeId);
  var newsIntro = contentNode.GetPropertyValue("newsIntro");
}

<p>@newsIntro</p>

With Umbraco 7 I used this code to get property from different pages:

@Umbraco.Content(1720).newsIntro

If the content item 1720 is a parent or ancestor of the page where you want to use the value, you can get it recursively like this:

@Umbraco.Field("newsIntro", recursive: true)

To get fields from content I have used this:

@{
    var selection = Umbraco.TypedContent(contentId).Children()
                        .Where(x => x.IsVisible())
                        .OrderBy("CreateDate");
}
@foreach(var item in selection){
            @item.GetPropertyValue("fieldName1")
            @item.GetPropertyValue("fieldName2")
            @item.GetPropertyValue("fieldName_N")
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!