开发者

umbraco.library.NiceUrl() returns error when passing a content node id

I have created a doctype in umbraco which has one of the following property:

Property - Case study link

Datatype - Content picker

I need to fetch the URL of this document in a Razor macro and assign it to a hyperlink.

Currently am doing it in this way but it's giving me an error:

@foreach (var item in @Model.OurWork){

          <a href="@umbraco.library.NiceUrl(item.caseStudyLink)">Read case study</a>               
}

And here is the error I get on viewing the page:

Error loading Razor Script OurWorkGrid.cshtml The best overloaded method match for 'umbraco.library.NiceUrl(int)' has some invalid arguments

I have tried outputting the node id witho开发者_如何学Pythonut using the niceURL() function and it works fine (outputs 1088).

<a href="@item.caseStudyLink">Read case study</a> 

results in this:

<a href="/1088">Read case study</a> 

But as soon as I put back NiceURL() function, it chokes again.

I really don't know what am I doing wrong here!


Instead of using the umbraco library method, try loading the node with the ID first, and then using the Url property to get the nice URL.

@foreach (var item in @Model.OurWork){
    var caseStudyNode = @Model.NodeById(item.caseStudyLink);
    <a href="@caseStudyNode.Url">Read case study</a>               
}

Also, add some form of a check to make sure the value is set, in case it's not a mandatory property on the doc type. Here's one example:

if (@item.HasProperty("caseStudyLink") && !string.IsNullOrEmpty(@item.caseStudyLink))
{
    ...
}


Try something like:

@foreach (var item in @Model.OurWork){

      <a href="@Model.NodeById(item.caseStudyLink).NiceUrl">Read case study</a>               
}

You may want to check first whether item.caseStudyLink contains a value because this will throw an error otherwise.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜