2014-10-18 58 views
0

我想如果用剃刀必須創建一個內聯如下:如何在剃鬚刀上使用內聯?

@(Model.ImageId == null ? "---" : @<text><img src='@(Url.Action(MVC.File.Get(Model.ImageId)))'/></text>) 

我不斷收到錯誤:

Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'lambda expression' 

cannot convert from 'System.Web.Mvc.ActionResult' to 'string' 

我怎樣才能解決這個使用內聯如果

回答

0

剃鬚刀做沒有一個很好的語法來輸出你所描述的內容。可以使用this answer中定義的內容。

如果這是我的代碼,我想用三元運算符,我可能會創建一個剃刀helper方法,在這種方式來使用它:

@Html.Raw(Model.ImageId == null ? "---" : OutputImageName(Model.ImageId).ToHtmlString()) 

@helper OutputImageName(string imageId /* change type to your actual type */) 
{ 
    <img src='@(Url.Action(MVC.File.Get(imageId)))'/> 
} 
0

試試這個代碼:

@(Model.ImageId == null ? "---" : string.Format(@"<text><img src='{0}'/></text>", Url.Action(MVC.File.Get(Model.ImageId)))) 

希望它幫助。

0
<text>@(Model.ImageId == null ? "" : string.Format("<img src='{0}'/>", Url.Action(MVC.File.Get(Model.ImageId))))</text> 
+0

雖然這可能會解決這個問題,但如果您提供了額外的解釋,這篇文章將會更有價值。 – 2014-10-18 17:32:23