雖然在尋找一個主題,我從果園CMS庫下載,我注意到一個Layout.cshtml文件在文件的頂部有下面的代碼塊:剃鬚刀文件中的@functions代碼塊會做什麼,以及何時(如果曾經)應該使用它?
@functions {
// To support the layout classifaction below. Implementing as a razor function because we can, could otherwise be a Func<string[], string, string> in the code block following.
string CalcuClassify(string[] zoneNames, string classNamePrefix)
{
var zoneCounter = 0;
var zoneNumsFilled = string.Join("", zoneNames.Select(zoneName => { ++zoneCounter; return Model[zoneName] != null ? zoneCounter.ToString() : ""; }).ToArray());
return HasText(zoneNumsFilled) ? classNamePrefix + zoneNumsFilled : "";
}
}
我知道聲明的函數做什麼(計算哪些區域被填充以便返回每列的寬度),我的問題是:@function塊的正確用法是什麼,以及我應該何時使用它?
個人而言,我認爲應該避免它,因爲它只是混淆了什麼是演示文稿/視圖層以及什麼是您的業務控制器/層。 – Liam
@李安 - 我不同意。我認爲它應該明確地用於*避免*混濁的水域。在這個例子中,它是爲了確定列的寬度,這不是控制器應該知道的。 – Bobson
@bobson指向 – Liam