'Razor Pages .net 6.0 generate Divs on server side
I want the program can generate Divs using tagbuilder or any methods in a for loop on theserver side. For exmaple. In my demo.cshtml.cs
private async Task LoadAsync()
{
for (int i = 0; i < 15; i++)
{
var div = new TagBuilder("div");
div.AddCssClass("template");
SOMECODE I DONT KNOW, NEED HELP
}
}
and in my cshtml, what code should I add to make those Divs appear.
Solution 1:[1]
You can try to use a html string:
[BindProperty]
public string htmlString{ get; set; };
private async Task LoadAsync()
{
for (int i = 0; i < 15; i++)
{
var div = "<div class=='template'></div>";
htmlString+=div;
}
}
And then if you want to use it in demo.cshtml,try to use @Html.Raw(Model.htmlString)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yiyi You |
