mvccontrib grid - 如何添加<tr> id

use*_*925 2 grid mvccontrib-grid asp.net-mvc-3

我想在我构建的mvccontrib网格的"tr"元素中添加一个id:

<tr id="0"/>
<tr id="1"/>
Run Code Online (Sandbox Code Playgroud)

因此,如果表包含10行,则id为0到9.

一种方法是向我的实体添加一个额外的项来存储这个值,然后将其创建为一个隐藏列,其id为该项的值 - 不是很优雅.

有没有更优雅的方式来做到这一点?谢谢

我已经做到这一点,但现在它在RenderUsing系列中抱怨,有什么想法吗?

@model  IEnumerable<Tens.Models.UserPreviousNamesView>

<div class="demo_jui">
@{   
var userId = 0;

foreach (var item in Model)
{
    userId = item.Id;
    break;
}


@(Html.Grid(Model.Select((item,index) => new { Item = item, Index = index}))
.Columns(col =>
{   
    col.For(p => p.Item.Title);
    col.For(p => p.Item.Name);        
    col.Custom(@<text>
                    @Ajax.ActionLink("Delete", "DeleteUserPreviousName", "Summary", null, null, new { id = item.Item.Id, @class = "deleteUserPreviousName" })                                                   
                </text>).Encode(false);
})
.RowAttributes(p => new Hash(Id => "id"+p.Item.Index.ToString()))
.Attributes(Id => "userPreviousNamesTable")
.Empty("You currently have no Previous Names.")
.RenderUsing(new Tens.GridRenderers.UserPreviousNamesGridRenderer<Tens.Models.UserPreviousNamesView>()));
Run Code Online (Sandbox Code Playgroud)

}

Dar*_*rov 5

您可以转换模型以将其添加到行索引,然后使用该RowAttributes方法:

@model IEnumerable<MyViewModel>
@(Html
    .Grid(Model.Select((item, index) => new { Item = item, Index = index }))
    .Columns(column =>
    {
        column.For(x => x.Item.Foo);
        column.For(x => x.Item.Bar);
    })
    .RowAttributes(x => new Hash(id => string.Format("id{0}", x.Item.Index)))
)
Run Code Online (Sandbox Code Playgroud)

此外,我已经使用id关键字预先设置了ID,因为HTML中的ID无法使用示例中显示的数字进行标记.

样本输出:

<table class="grid">
    <thead>
        <tr>
            <th>Foo</th>
            <th>Bar</th>
        </tr>
    </thead>
    <tbody>
        <tr id="id0" class="gridrow">
            <td>foo 1</td>
            <td>bar 1</td>
        </tr>
        <tr id="id1" class="gridrow_alternate">
            <td>foo 2</td>
            <td>bar 2</td>
        </tr>
        <tr id="id2" class="gridrow">
            <td>foo 3</td>
            <td>bar 3</td>
        </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)