【问题标题】:Razor in-line if statement not working?剃刀内联if语句不起作用?
【发布时间】:2011-06-01 19:18:27
【问题描述】:

我在这条线上抛出了一个异常,不知道为什么......也许其他人可以发现它

<img src="@{Model.Image != null ? Model.Image.FileName : "";}" width="200px" id="ImagePreview"/>

我得到的例外是:

error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

【问题讨论】:

    标签: asp.net-mvc-3 razor


    【解决方案1】:

    您需要为该表达式使用表达式 (explicit) 代码块样式:

    <img src="@(Model.Image != null ? Model.Image.FileName : "")" width="200px" id="ImagePreview"/>
    

    gu's post

    【讨论】:

    • 圆括号前不要加半列(;)
    • 这解决了我试图返回文字的问题。我正在使用@{}。谢谢!
    【解决方案2】:

    尝试用括号代替花括号:

    <img src="@(Model.Image != null ? Model.Image.FileName : "")" width="200px" id="ImagePreview"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2019-12-29
      • 2012-07-16
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多