The loop pattern is...
Func<dynamic, object> myUnorderedListTemplate =
@<ul>
@foreach (var i in item)
{
<li>@i.Name</li>
}
</ul>;
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
The input parameter in the following Templated Razor Delegate is dynamic. I expected I didn't have any errors before executing the program. because it is a dynamic type and it execute at run time if we misspell the name.
@page
@model WebApplication1.Pages.WelcomeModel
@{
Func<dynamic, object> myUnorderedListTemplate =
@<ul>
@foreach (var item in cities)
{
<li>@item.Name</li>
}
</ul>;
}
@myUnorderedListTemplate(citiesA) @*when added A to cities, the compiler got an error.
The variable is dynamic and I did not expect the error before execution.*@
@functions {
public class City
{
public string Name { get; set; }
public string Country { get; set; }
}
List<City> cities = new List<City>{
new City { Name = "London", Country = "UK" },
new City { Name = "Paris", Country = "France" },
new City { Name = "Rome", Country = "Italy" }
};
}
The loop pattern is...
Func<dynamic, object> myUnorderedListTemplate =
@<ul>
@foreach (var i in item)
{
<li>@i.Name</li>
}
</ul>;
dynamic is similar to object in that its type can be changed. what's different is that the properties and methods are executed at runtime via reflection.
in your code:
Func<dynamic, object> myUnorderedListTemplate =
@<ul>
@foreach (var item in cities).
{
<li>@item.Name</li>
}
</ul>;
you have a couple of issues:
while a helper is cleaner, you can:
Func<IEnumerable<dynamic>, HtmlString> myUnorderedListTemplate = (cities) =>
{
var html = "<ul>";
foreach (var item in cities)
{
html += $"<li>{System.Web.HttpUtility.HtmlEncode(item.Name)}</li>";
}
html += "</ul>";
return new HtmlString(html);
};
though as the function needs to know the property names, not sure why dynamic.
in your code calling the function the variable citiesA is undefined.
@myUnorderedListTemplate(citiesA)
Hi @Shervan360 ,
Func<dynamic, object>
The input parameter represents data and is a dynamic type, the data is accessible within the template through a parameter named item. When you pass citiesA, you need to define citiesA first, the full code like :
@page
@model WebApplication1.Pages.WelcomeModel
@{
Func<dynamic, object> myUnorderedListTemplate =
@<ul>
@foreach (var city in item)
{
<li>@city.Name</li>
}
</ul>;
}
@myUnorderedListTemplate(citiesA) @*when added A to cities, the compiler got an error.
The variable is dynamic and I did not expect the error before execution.*@
@functions {
public class City
{
public string Name { get; set; }
public string Country { get; set; }
}
List<City> cities = new List<City>{
new City { Name = "London", Country = "UK" },
new City { Name = "Paris", Country = "France" },
new City { Name = "Rome", Country = "Italy" }
};
List<City> citiesA = new List<City>{
new City { Name = "London", Country = "UK" },
new City { Name = "Paris", Country = "France" }
};
}
result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Qing Guo