2

When the following ASP.NET MVC 5 Razor code is used in a view

@(Model.cobrandingLogoList[i].isMine ? " class=\"logo-owner\"" : String.Empty)

The rendered output is

class=""logo-owner""

Edit: added screen capture of the output enter image description here

The same thing happens if I use

@(Model.cobrandingLogoList[i].isMine ? @" class=""logo-owner""" : String.Empty)

I was expecting this

class="logo-owner"

What should be used to get the desired result?

ChrisP
  • 9,796
  • 21
  • 77
  • 121
  • Don't use any quotes around the class name - i.e. `" class=logo-owner"`. – petelids Jan 28 '15 at 23:00
  • You expect this but what was the result ? – mybirthname Jan 28 '15 at 23:03
  • 1
    The result of the first snippet is very weird. Are you sure that this particular code generates this particular output? Can you reproduce this behavior in a new project? – Theodoros Chatzigiannakis Jan 28 '15 at 23:09
  • @mybirthname the result follows "The rendered output is" text in the question. – ChrisP Jan 28 '15 at 23:18
  • @petelids according to the W3C HTML attribute value such as class should enclose the values in double quotes http://www.w3schools.com/tags/att_global_class.asp – ChrisP Jan 28 '15 at 23:19
  • My point (which wasn't well made) was that razor will put the quotes in for you I think. – petelids Jan 28 '15 at 23:23
  • @TheodorosChatzigiannakis See the image capture in the edit of the question showing the rendered output. – ChrisP Jan 28 '15 at 23:25
  • @petelids Ahh. That is likely what is going on here. What is surprising is that the entire string is being inserted "class=..." not just the value for an existing attribute. Maybe Razor is that intelligent? – ChrisP Jan 28 '15 at 23:26
  • Can you put more full code in? This is presumably happening inside an HTML element and not just on its own as quoted?ie is it something like `` ? – Chris Jan 28 '15 at 23:29
  • 1
    Razor has some mechanics to try and be "helpful" with attributes that are driven by model values. This sometimes gets in the way as you've found. The linked question above covers several of the techniques to use a ternary to control the output of an attribute. – AaronLS Jan 28 '15 at 23:34

0 Answers0