Model.Content.GetPropertyValue 而不是 Umbraco.Field [英] Model.Content.GetPropertyValue instead of Umbraco.Field
问题描述
这是我的代码的一部分:
@using Umbraco.Web;@inherits Umbraco.Web.Mvc.UmbracoTemplatePage@{var mTest = Model.Content.GetPropertyValue("info", true);}<div>@mTest
无法获取info
属性的内容.我收到以下错误:未将对象引用设置为对象的实例.另一方面,使用以下方法一切正常:
@Umbraco.Field("info", recursive: true)
但是,我想使用第一种方法.如果您对此有任何帮助,我将不胜感激.
我相信错误.
Model.Content 与 Currentpage 相同.我的猜测并且我相信这是一个很好的方法,即您要查找的值不在当前页面上,而可能在母版页/子/祖先/父页面中.
我认为您需要的是遍历节点的家族"并找到该值.例如,如果您在主页上有该值,并且您希望它显示在子页面上:
Model.Content.Ancestor(1).GetPropertyValue("info");
这只是一个例子.从你已经说过的话,我不能告诉你很多你想要做什么.正如其他人评论的那样,您的代码看起来不错.
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.
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.
这篇关于Model.Content.GetPropertyValue 而不是 Umbraco.Field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!