I have some MVC3 Razor code. I am not so familiar with Razor but I can understand what the code is doing. I would like to clean this code up. Is there anything that could be done or is what I have the best possible?
@{ var i=1; foreach (var topic in @Model.Topic)
{
<option value="@topic.RowKey">@(i++). @topic.Description</option>
}
}
Personally, I’d merge the selection of the index of the item with the item in the sequence while iterating, like so:
This way, you don’t have separation the indexing logic from the topic, it’s all contained neatly within the anonymous type.