編輯 Razor 視圖時,Visual Studio 中的編輯器出現問題。我正在使用 ASP.NET Core 3.1 版。我有在幾個地方重復的相同 HTML,所以我想創建一個函式來完成它。本文描述了它應該如何完成:
uj5u.com熱心網友回復:
好的,所以我找到了解決方法。它是在函式頂部放入一個以分號結尾的陳述句:
@{
void MakeDiv(string subtitle, string[] props, string style)
{
var a = 1; //this would fix it
<div style="@style">
<h3>@subtitle</h3>
<table>
@foreach (var e in Model.Egenskaper
.Where(e => props.Contains(e.QId)))
{
<tr>
<td>
<a href="http://#@e.QId">@e.Name</a>
</td>
<td>
@e.Value
</td>
</tr>
}
</table>
</div>};
}
}
但我想檢查是否有任何道具可以展示會更有意義。只要函式中的第一條陳述句不是 HTML,而是以分號結尾的代碼。
我仍然不確定為什么會發生這種情況。也許是因為一些插件(例如,我使用的是 Resharper)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415883.html
標籤:
上一篇:從共享布局中檢索資料庫值
