Model.Content.GetPropertyValue instead of Umbraco.Field

半腔热情 提交于 2019-12-13 20:23:32

问题


Here is part of my code:

@using Umbraco.Web;
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{
    var mTest = Model.Content.GetPropertyValue<string>("info", true);
}
<div>
    @mTest
</div>

It is impossible to get the content of info property. I am getting the following error: Object reference not set to an instance of an object. From the other side, everything works fine by using:

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

However, I want to use the first approach. I'd appreciate any help on that.


回答1:


I believe the error.

Model.Content is the same as Currentpage . My guess and I believe it is a good one is that the value you're looking for is not on the current page but maybe in a master page/child/ancestor/parent.

I think what you need is to traverse through the 'family' of nodes and find that value. If you had that value on the home page for example and you wanted it to show on a child page:

Model.Content.Ancestor(1).GetPropertyValue("info");

This is just an example. I can't tell a lot on what you're trying to do from what you said already. As others have commented, your code seems fine.



来源:https://stackoverflow.com/questions/49193753/model-content-getpropertyvalue-instead-of-umbraco-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!