2011-06-01 48 views
25

我有一個例外,在這條線投擲,無法弄清楚,爲什麼......也許別人可以發現它如果語句不起作用,剃刀內嵌?

<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 

回答

60

您需要使用表達式(明確)鱈魚對於表達式e塊風格:

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

看到gu's post

+2

不添加半支柱(;)關閉括號 – Mathieu 2013-01-09 14:23:49

+0

這解決了試圖返回文字我的問題了。我正在使用@ {}。謝謝! – 2013-12-26 06:04:56

9

嘗試在括號,而不是大括號包裹它:

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