0

I can't understand why the following code doesn't work

@{
    bool isDisabled = true; // I have omitted the if-statement that makes this true
<select class="form-control"
        asp-items="Model.Locations"
        asp-for="@Model.Id"

    @if (isDisabled)
        {
            @Html.Raw("disabled")
        }

>

</select>
}

The HTML renders without the word "disabled" inside the end of the tag. It's as though Html.Raw can't be used here. It works if I take it outside of the tag i.e. I can get it to render as <Select ... >disabled</select> - just not <select ... disabled ></select>

I basically want to set the bool to true on a certain condition which means that the field should be un-editable. I thought this would be the most straightforward way!

egmfrs
  • 1,272
  • 3
  • 17
  • 31
  • 1
    Why are you wanting to do this - disabled controls do not submit a value so will likely cause your model binding to fail. And why render all that extra html when all you want is to display the value. –  Jan 30 '18 at 23:38
  • The user fills out a form. They press next, the data is saved. If they navigate back to the page, they have the opportunity to edit some of the fields (this is by request of the client) i.e. some questions you're not allowed to go back and change your mind on. I don't understand what you mean by "all that HTML"? I wan't the form to still appear like a form, and it be obvious to the user which fields are no longer editable. From your duplicate answer I was able to implement a sound solution. I've had no issues with model binding. The editable fields are saved by sending a PUT request to an API. – egmfrs Feb 02 '18 at 23:44
  • I mean why generate a ` –  Feb 02 '18 at 23:46
  • Is it really any better to have... if (noExistingData) { – egmfrs Feb 03 '18 at 00:17
  • In my opinion it is :) –  Feb 03 '18 at 00:18
  • 1
    You can just use ` – jesse Dec 30 '19 at 13:55
  • I can confirm this works (I have been using it for nearly 2 years, forgot I had ever asked this question :)) – egmfrs Jan 02 '20 at 16:42

0 Answers0