问题
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