2014-10-18 59 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