.Net Razoe view rendering code puts some restrictions on when writing attributes in Tags that have dependency on dynamic model values or some other run time values. For example when rendering option tags under select tag, you may have one or more options that are selected. That will require you to add "selected" attribute to option element.
<select> <option selected="">value 1</option> <option>value 1</option> </select>
In above example first option has "selected" attribute to indicate this option needs to be shown selected when dropdown renders. One way to write the above HTML in razor view can be as below.
<option value="@myOption.Value" @myOption.Selected>@myOption.Text</option>
This view will not compile. It will throw following error.
error RZ1031: The tag helper 'option' must not have C# in the element's attribute declaration area.
This is by design as mentioned in .Net Razor Documentation. As per documentation you can't have C# in attribute area. But you can use C# to set value of the attribute. If C# expression in value section of the attribute evaluates to false, .Net framework will not render that attribute. You can rewrite above Razor code as below.
<option value="@myOption.Value" selected="@myOption.Selected">@myOption.Text</option>
The above code will render "selected" attribute if the property myOption.Selected will evaluate to true. Otherwise .Net framework will not render selected attribute in option tag.