自定义DisplayFor输出

时间:2015-09-17 19:56:39

标签: templates asp.net-mvc-5.2 displayfor

我正在稳步前进:) 所以,我已经获得了包含产品列表的精彩网格! 现在,有一个非常糟糕的复选框,显示为布尔值 我宁愿有一个是/否的东西。所以我用谷歌搜索了这篇精彩的文章:http://buildstarted.com/2010/09/10/overriding-displayfor-and-editorfor-to-create-custom-outputs-for-mvc/ 问题是他显示的输出不是有条件的,所以我的代码 看起来像这样:

@inherits System.Web.Mvc.WebViewPage<string>
@if @Model == True
{
  <span>Yes</span>
}
@else{
  <span> No </span>
}

但是VS 2013对它来说都很潦草,这通常是一个不好的预兆! 当我尝试运行它时,我看到复选框。嘿......很多东西要学...... 无论如何,我注意到了一个类似的问题,即为什么DisplayFor模板没有显示。我查看了OP的代码,发现他已经放置了我的属性没有的DataType属性(现在已经有了)。
但是,当我尝试在我的模型中使用DataType属性时,VS又一次变得麻木了 - 不好预兆! 当我试图编译时,geez,所有地狱都崩溃了,我得到了6个编译错误(但这至少取得了进展,只是忽略了一切!)。 错误是这些:

Error   1   Expected a "{" but found a "@".  Block statements must be enclosed in "{" and "}".  You cannot use single-statement control-flow statements in CSHTML pages. For example, the following is not allowed:

@if(isLoggedIn)
    <p>Hello, @user</p>

Instead, wrap the contents of the block in "{}":

@if(isLoggedIn) {
    <p>Hello, @user</p>
}   c:\Users\Andrea\Documents\Visual Studio 2013\Projects\ARSoftLabs.it\ARSoftLabs.it\Views\Products\DisplayTemplates\ProductHasTrial.cshtml    2   4   ARSoftLabs.it
Error   2   ) expected  c:\Users\Andrea\Documents\Visual Studio 2013\Projects\ARSoftLabs.it\ARSoftLabs.it\Views\Products\DisplayTemplates\ProductHasTrial.cshtml    1   1   ARSoftLabs.it
Error   3   Syntax error, '(' expected  c:\Users\Andrea\Documents\Visual Studio 2013\Projects\ARSoftLabs.it\ARSoftLabs.it\Views\Products\DisplayTemplates\ProductHasTrial.cshtml    2   4   ARSoftLabs.it
Error   4   Invalid expression term 'else'  c:\Users\Andrea\Documents\Visual Studio 2013\Projects\ARSoftLabs.it\ARSoftLabs.it\Views\Products\DisplayTemplates\ProductHasTrial.cshtml    6   2   ARSoftLabs.it
Error   5   The type or namespace name 'DataTypeAttribute' could not be found (are you missing a using directive or an assembly reference?) c:\Users\Andrea\Documents\Visual Studio 2013\Projects\ARSoftLabs.it\ARSoftLabs.it\Models\Product.cs 20  10  ARSoftLabs.it
Error   6   The type or namespace name 'DataType' could not be found (are you missing a using directive or an assembly reference?)  c:\Users\Andrea\Documents\Visual Studio 2013\Projects\ARSoftLabs.it\ARSoftLabs.it\Models\Product.cs 20  10  ARSoftLabs.it

所以基本上我认为它告诉我,我不知道关于Razor的杰克,这是非常坦率的。无论如何,正在使用System.ComponentModel,因为DisplayName属性的工作方式类似于魅力,MSDN声称DataType在同一个程序集中。

想法?指针?

谢谢!

A

0 个答案:

没有答案
相关问题