0

I am trying to create an inline if with razor has follows:

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

I keep getting the errors:

 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'

How can I solve this using an inline if

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

3 Answers3

0

Razor does not have a nice syntax to output what you described. It is possible to use what is defined in this answer.

If it was my code I wanted to use ternary operator I would probably create a Razor helper method and use it in such way:

@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)))'/>
}
Community
  • 1
  • 1
dotnetom
  • 24,551
  • 9
  • 51
  • 54
0

Try this code:

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

Hope it helps.

Bonomi
  • 2,541
  • 5
  • 39
  • 51
0
<text>@(Model.ImageId == null ?  "" : string.Format("<img src='{0}'/>", Url.Action(MVC.File.Get(Model.ImageId))))</text>
Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
  • While this may solve the question, this post would be much more valuable if you provided additional explanations. – jb. Oct 18 '14 at 17:32