我想将id添加到我构建的mvccontrib网格的"tr“元素中:
<tr id="0"/>
<tr id="1"/>因此,如果表包含10行,则if从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>()));}
发布于 2012-02-24 20:25:39
您可以转换模型以添加行索引,然后使用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)))
)另外,我已经在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>发布于 2014-08-23 02:19:58
您可以始终显示隐藏列,而无需向特定行或列添加id,如下所示
$(".mvcGridDollarHeader th:nth-child(16)").hide();
$(".mvcGrid td:nth-child(16)").hide();其中mvcGrid为tableStyle,mvcGridDollarHeader为标头样式。
@grid1.GetHtml(
tableStyle: "mvcGrid",
displayHeader: true,
emptyRowCellValue: "",
headerStyle: "mvcGridDollarHeader",https://stackoverflow.com/questions/9430538
复制相似问题