Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're looking for a helpers that will take a string and:</p> <ol> <li>If the string is not empty, render that string.</li> <li>If the string is not empty, render a given template.</li> <li>If the string is empty, render "N/A" html.</li> <li>If the string is empty, render a given template.</li> </ol> <p>When passing a razor block to a function as a parameter, razor packages the block as Func. Change the parameters in the helper functions to take that type of delegate and don't forget to call those delegates (I chose to pass null).</p> <p>These helpers should handle those scenarios.</p> <p><strong>Solution</strong></p> <pre><code>@helper RenderThisOrThat(string stringToRender, Func&lt;object, IHtmlString&gt; leftTemplate = null, Func&lt;object, IHtmlString&gt; rightTemplate = null) { var shouldRenderLeft = !string.IsNullOrWhiteSpace(stringToRender); leftTemplate = leftTemplate ?? (o =&gt; MvcHtmlString.Create(stringToRender)); @RenderThisOrThat(shouldRenderLeft, leftTemplate, rightTemplate) } @helper RenderThisOrThat(bool shouldRenderLeft, Func&lt;object, IHtmlString&gt; leftTemplate, Func&lt;object, IHtmlString&gt; rightTemplate = null) { var shouldRenderRight = !shouldRenderLeft; if (shouldRenderRight) { if (rightTemplate != null) { @rightTemplate(null) } else { &lt;span class='muted'&gt;N/A&lt;/span&gt; } } else { @leftTemplate(null) } } </code></pre> <p><strong>Examples</strong></p> <pre><code>1. @Helpers.RenderThisOrThat(Model.StringWithBob) 2. @Helpers.RenderThisOrThat(Model.StringWithNull) 3. @Helpers.RenderThisOrThat(Model.StringWithBob, @&lt;span&gt;I'm @Model.StringWithBob&lt;/span&gt;) 4. @Helpers.RenderThisOrThat(Model.StringWithNull, @&lt;span&gt;I'm @Model.StringWithBob&lt;/span&gt;) 5. @Helpers.RenderThisOrThat(Model.StringWithBob, @&lt;span&gt;I'm @Model.StringWithBob&lt;/span&gt;, @&lt;span&gt;What about Bob?&lt;/span&gt;) 6. @Helpers.RenderThisOrThat(Model.StringWithNull, @&lt;span&gt;I'm @Model.StringWithBob&lt;/span&gt;, @&lt;span&gt;What about Bob?&lt;/span&gt;) </code></pre> <p>Will output:</p> <ol> <li><code>Bob</code></li> <li><code>&lt;span class='muted'&gt;N/A&lt;/span&gt;</code></li> <li><code>&lt;span&gt;I'm Bob&lt;/span&gt;</code></li> <li><code>&lt;span class='muted'&gt;N/A&lt;/span&gt;</code></li> <li><code>&lt;span&gt;I'm Bob&lt;/span&gt;</code></li> <li><code>&lt;span&gt;What about Bob?&lt;/span&gt;</code></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload