I have a section of Razor code in one of my views that does not behave as I expect. This code contains several places where I need to add the value of a variable from the model to the html attribute. The code below is a slightly simplified version of the markup on my page (I removed some “markup” noise that was not important for this example, and just made the code more difficult to read).
There are three cases when I use @topic.StandardTopicId. The first and second instances are replaced as I expected, but the third instance name="IsCovered_@topic.StandardTopicId"displays without changing the variable.
Code in question:
@foreach(var topic in Model.Group) {
<div id="frame-@topic.StandardTopicId" class="topic-frame">
<label>
<input type="radio" name="IsCovered_@(topic.StandardTopicId)" />
No
</label>
<label>
<input type="radio" name="IsCovered_@topic.StandardTopicId" />
Yes
</label>
</div>
}
An example of output from the above code block:
...
<div id="frame-42" class="topic-frame">
<label>
<input type="radio" name="IsCovered_42" />
No
</label>
<label>
<input type="radio" name="IsCovered_@topic.StandardTopicId" value="No" />
Yes
</label>
</div>
...
- , ?