显示模板的行为在 MVC5 中是否从 MVC3 发生了变化?

Has behaviour of Display Templates changed in MVC5 from MVC3?

我已经从 MVC3/Razor 升级到 MVC5/Razor。

我发现我的显示模板不再读取模型值。

模板名为 "MyTemplate":

@model System.Object
@{
if (Model !=null)
{
    string.Format("{0:0.00}", Model);
}
else
{
    @:-    
}

}

调用者:

@Html.DisplayFor(modelItem => item.mydecimalvalue, "MyTemplate")

当我通过调试器跟踪它时,模型返回空值。它曾经在 MVC3

中运行良好

如有任何帮助,我们将不胜感激。

编辑 1

string.Format("{0:0.00}", Model);

应该是:

@(string.Format("{0:0.00}", Model));

MVC4 或 5 一定对此进行了更改,因为它曾经有效。

如果您使用 Html.FormatValue,它会起作用,因为它是 Razor 中 String.Format 的首选方式。

@model System.Object
@{
    if (Model != null)
    {
        @Html.FormatValue(Model, "0.00")
    }
    else
    {
        @:-
    }

}

参考:

using string.Format in MVC Razor view page

As you have discovered, it is possible to use String.Format, but it is a PITA because of the strange (non-intuitive) syntax needed. For that reason, Html.Format (a razor extension) is cleaner and IMO, more readable.