MVC3 Close Tags Dyanmically
I am probably breaking some convention here. I have a dictionary coming from a model, whose pair count is unknown. I want to display all the key/value pairs, but break the displayed columns in counts of five. I thought this would work:
<div class="substatus_group">
<span class="title">Substatus</span>
@{int i =0}
@foreach (var item in Model.substatus)
{
if(i > 1 && i % 5 == 0)
{
</div><div>
}
<span class="rtm_substatus">@item.Key: @item.Value</span>
i++;
}
</div>
Instead, the bracket on the closing div says its missing its start, and the if loop says its missing its closing bracket. The page never renders.
Obviously, Razer is not interpreting the code as its written, choosing to mark something as html/code when it is the opposite. I would like to know开发者_如何学Go how to force Razer to render this properly. If a better solution to dynamically creating tags exists, that would also be a good answer.
How about this? Use a for loop to manage the index, and check the modulus for every iteration instead of skipping the first one.
It appears that MVC3/Razor doesn't like having start and end tags in different scopes.
I was able to solve this by using a partial view and some Linq to partition the dictionary into different "chunks".
Your view:
@{
string[] keys = Model.Keys.ToArray();
for (int i = 0; i < keys.Length; i += 5)
{
Html.RenderPartial("_partialView", Model.Skip(i).Take(5));
}
}
Your partial view:
<div class="substatus_group">
@foreach (KeyValuePair<string, string> item in Model)
{
<span class="title">Substatus</span> <span class="rtm_substatus">@item.Key: @item.Value</span>
}
</div>
精彩评论