Reputation: 8783
I have two method helper with theses signatures:
@helper CreateNavigation(int parentId, int depthNavigation)
@helper Navigation(int parentId, int depthNavigation)
I have tried to convert it to a proper method for using in ASP.NET Core 2
.
But I got an error in VS2017 IDE
that is:
1- Use of unassigned local variable 'Navigation'
2- local variable 'Navigation' might not be initialized before accessing
How can I fix it?
@using Microsoft.AspNetCore.Html
@using Microsoft.AspNetCore.Mvc.Razor
@model List<Jahan.Beta.Web.App.Models.Comment>
@{
Func<int, int, int, HelperResult> CreateNavigation
= (parentId, depthNavigation) => new Func<object, HelperResult>(
@<text>@{
Comment parent = Model.SingleOrDefault(r => r.Id == parentId);
depthNavigation = 6;
@Navigation(parentId, depthNavigation)
}</text>)(null);
}
@{
Func<int, int, HelperResult> Navigation
= (parentId, depthNavigation) => new Func<object, HelperResult>(
@<text>@{
var parent = Model.Children(parentId);
if (//condition)
{
if (//condition)
{
foreach (var comment in Model)
{
if (//condition)
{
<li><p>@comment.Description</p></li>
@Navigation(comment.Id, depthNavigation)
}
}
}
}
}</text>)(null);
}
Upvotes: 1
Views: 809
Reputation: 41
I had the same problem and I was able to solve it by declaring the delegate first and setting it to null, then adding the implementation like this:
@{Func<IEnumerable<Foo>, IHtmlContent> DisplayTree = null;}
@{DisplayTree = @<text>@{
var foos = item;
<ul>
@foreach (var foo in foos)
{
<li>
@foo.Title
@if (foo.Children.Any())
{
@DisplayTree(foo.Children)
}
</li>
}
</ul>
}</text>;}
@DisplayTree(new List<Foo>{...})
Upvotes: 2