0

I'm trying to do something pretty straight forward in a Composite C1 function but I believe the fact that Composite treats the function's HTML output as XML is causing issues. Unfortunately I cannot find a way around this one. So here's the offending line of code:

<input type="radio" name="@inputName" value="@radioLabel" required="@required" @(radioLabel.Trim() == inputValue.Trim() ? @"checked=""checked""" : "") />

The ternary function at the end is actually the problem. The output should end up being

checked = "checked"

but instead the quotes are turned into entities and it ends up being

checked=&quot;checked&quot; 

Here's a picture of the error enter image description here


Well, actually there is a simple way around this issue using the standard if/else logic below, but I would still like to know the answer to this question as I've had more than a few Composite C1-isms like this in the past and would like a solution.

@if(radioLabel.Trim() == inputValue.Trim())
{
  <input type="radio" name="@inputName" value="@radioLabel" required="@required" checked="checked" />
}
else
{
  <input type="radio" name="@inputName" value="@radioLabel" required="@required" />
}
Crob
  • 599
  • 4
  • 26

2 Answers2

1

How about using Html.Raw():

<input type="radio" name="@inputName" value="@radioLabel" required="@required" @(radioLabel.Trim() == inputValue.Trim() ? Html.Raw("checked=\"checked\"") : Html.Raw(""))  />

And also the if block instead of the ternary function like this:

<input type="radio" name="@inputName" value="@radioLabel" required="@required" @if(radioLabel.Trim() == inputValue.Trim()) { @Html.Raw("checked=\"checked\"") } />

I am not an expert in Razor but hope this would help :)

wysocki
  • 731
  • 5
  • 7
  • That actually does work. I had tried Raw before, but used a ToString. Since Raw outputs an IHtmlString apparently Composite won't touch it? Thanks wysocki – Crob Mar 04 '15 at 14:03
  • Or maybe I'm just an idiot and this was really a Razor problem and I kept thinking it was Composite causing the issue ;) – Crob Mar 04 '15 at 14:21
1

You're doing it wrong and treat Razor like its nothing but a StringBuilder though its much smarter than that. Especially in this case, you should use Conditional Attributes

The correct code is NOT to use Html.Raw, but to set your attribute-value to null if you don't want it to be rendered.

@{
    string checked = radioLabel.Trim() == inputValue.Trim() ? "checked" : null;

   <input type="radio" name="@inputName" value="@radioLabel" required="@required" checked="@checked" />
}
Community
  • 1
  • 1
Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48
  • Thanks Pauli, works perfectly. I've used conditional attributes in Razor before but didn't realize the Composite Razor engine had them. I incorrectly assumed that because MVC 3 is mentioned all over the documentation that the version of Razor coincided with that and I believe Conditional Attributes were released with the version of Razor (2?) in MVC4. Thanks for the info. That link you provided looks very familiar :) – Crob Mar 04 '15 at 15:32
  • 1
    Razor != MVC != WebPages. Its all very confusing and you Microsoft is clearly to blame for this. Composite C1 uses the WebPages engine, which sits on top of Razor. MVC itself sits on top of WebPages. So everything you can do in Razor or WebPages, you can do in Composite C1 Razor functions. – Pauli Østerø Mar 04 '15 at 16:02