Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a bug in MVC3 Razor @if
    primarykey
    data
    text
    <p>I have several form fields that may or may not need to be displayed, depending on the entity for which the form is loaded. So in my model I have boolean properties assigned accordingly.</p> <p>Each field in the form has a corresponding "label" (not an html Label). The following code hides the html input element, but not the label !?!?</p> <pre><code>&lt;!-- DBA: _________________ --&gt; &lt;tr&gt; &lt;td&gt; @if (Model.DisplayDBAField) { @Html.Raw("DBA:") } &lt;/td&gt; &lt;td&gt; @Html.TextBox("DoingBusinessAs", Model.DoingBusinessAs, new { style = string.Format("visibility: {0};", Model.DisplayDBAField ? "visible" : "hidden") }) &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>In order to get the desired display, which is for both, the label and the input to not display, I had to rewrite the above as follows:</p> <pre><code>&lt;!-- DBA: _________________ --&gt; &lt;tr&gt; &lt;td&gt; @Html.Raw(Model.DisplayDBAField ? "DBA:" : "") &lt;/td&gt; &lt;td&gt; @Html.TextBox("DoingBusinessAs", Model.DoingBusinessAs, new { style = string.Format("visibility: {0};", Model.DisplayDBAField ? "visible" : "hidden") }) &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>For some reason, the code block [ @if (Model.DisplayDBAField){} ] NEVER evaluates to true. Is this a Razor bug?, Visual Studio bug? Asp.Net bug?</p> <p>Per MilkyWayJoe's request: here's the code in the controller:</p> <pre><code>if (!string.IsNullOrEmpty(hiddenFields)) { profile.DisplayDBAField = !hiddenFields.Contains("DBA"); profile.DisplayVendorIDField = !hiddenFields.Contains("VendorID"); profile.DisplayContactNameFields = !hiddenFields.Contains("ContactName"); } </code></pre> <p>The code above works fine, I step through it and the fields are set/unset correctly. And here's the model:</p> <pre><code>public class ProfileModel { public EntityProfile Entity { get; set; } public bool DisplayDBAField { get; set; } public bool DisplayVendorIDField { get; set; } public bool DisplayContactNameFields { get; set; } public bool DisplayDistributionMethodField { get; set; } public bool DisplayCardPurposeField { get; set; } public ProfileModel() { DisplayDBAField = true; DisplayVendorIDField = true; DisplayContactNameFields = true; DisplayDistributionMethodField = true; DisplayCardPurposeField = true; } } </code></pre> <p>So the default constructor sets the flags to true, and the controller modifies the flags when appropiate. I also tried the following in the view:</p> <pre><code> &lt;td&gt; @if (!Model.DisplayDBAField) { @Html.Raw("DBA:") } &lt;/td&gt; </code></pre> <p>And found that regardless of the value in Model.DisplayDBAField, be it true or false, "DBA:" is NEVER rendered! So, whether I use @if (Model.DisplayDBAField){} or @if (!Model.DisplayDBAField){} it seems the code itself is completely ignored at runtime.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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