我想在Razor helper内核中使用.net
我使用这个代码
@{
void DrawRow(CategoryModel child, int parentNo)
{
@<text>
<tr style="display: none;">
<td><span class="treegrid-indent"></span><span class="treegrid-expander"></span>@child.Name</td>
</td>
</tr>
</text>;
}
}但是当使用这个时,得到错误
"@“字符后面必须有":”、"(“)或C#标识符。如果要切换到标记,请使用HTML开始标记,
发布于 2020-02-12 09:20:09
您可以在视图组件核心应用程序中使用ASP.NET实现相同的需求,如下所示。
ViewComponent类
[ViewComponent(Name = "DrawRow")]
public class DrawRowComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(DrawRowModel model)
{
return View(model);
}
}ViewComponent Razor视图
@model DrawRowModel
@*display: none;*@
<tr style="">
<td>
<span class="treegrid-indent"></span>
<span class="treegrid-expander"></span>
@Model.child.Name
</td>
</tr>模型类(Es)
public class CategoryModel
{
public string Name { get; set; }
}
public class DrawRowModel
{
public CategoryModel child { get; set; }
public int parentNo { get; set; }
}调用Test视图页面中的视图组件
@{
ViewData["Title"] = "Test";
var model = new DrawRowModel { child = new CategoryModel { Name = "Category" }, parentNo = 0 };
}
<h1>Test</h1>
@for (int i = 1; i < 6; i++)
{
model.child.Name = "Category" + i.ToString();
model.parentNo = i;
<table>
@await Component.InvokeAsync("DrawRow", model)
</table>
}测试结果

https://stackoverflow.com/questions/60073265
复制相似问题