MVC需要在剃刀视图中混合使用string和c#代码

时间:2014-01-21 11:20:31

标签: c# asp.net-mvc razor

我需要证明这一点:

显示Model.AgreementsList.Count < Model.Filter.DisplayLastAgreementsCount ? Model.AgreementsList.Count : Model.Filter.DisplayLastAgreementsCount

@Model.AgreementsList.Count

我需要在cshtml文件中写什么? 我使用@{}并没有找到解决方案。 感谢

2 个答案:

答案 0 :(得分:3)

使用@()(使用@{}仅执行附带的代码,不会向响应流写入任何内容)

@(Model.AgreementsList.Count < Model.Filter.DisplayLastAgreementsCount ? Model.AgreementsList.Count : Model.Filter.DisplayLastAgreementsCount)

但是,不是在视图中执行此类操作,您可能最好使用ViewModel,甚至使用ViewBag并从Controller分配此输出。

答案 1 :(得分:0)

使用@:来编写字符串,使用@来从模型中检索数据。