我正在使用MVC Contrib Grid来渲染,排序和过滤我的数据网格,但我现在遇到了问题,因为它已经升级为MVC3和Razor。我的columns集合中的Custom方法不适用于aspx页面,Action方法列现已过时。
我使用网格操作方法在MVC 2中渲染我的列:
...
<% Html.Grid(Model).Columns(column => {
column.For(x => x.Id);
column.For(x => x.Name);
column.For(x => x.Email);
column.For(x => x.DateOfBirth);
column.For("View User").Named("Tools").Action(x => { %>
<td>
<%= Html.ActionLink("View", "View", new { id = p.Id })%>
<%= Html.ActionLink("Edit ", "Edit", new { id = p.Id })%>
//Snip as there are too many tools :-)
//.....
<%= Html.ActionLink("Delete", "Delete", new { id = p.Id })%>
</td>
<% });
...
现在有了最新版本,有一个Custom方法可以取代过时的Action方法。我看看它是如何完成的in here,它现在基本上对我有用,但我在aspx视图(url等)中放松了所有助手,现在需要在我的模型中用另一种方法渲染我的内容如下所示:
...
<% Html.Grid(Model).Columns(column => {
column.For(x => x.Id);
column.For(x => x.Name);
column.For(x => x.Email);
column.For(x => x.DateOfBirth);
//the new custom column
column.Custom(Model.ToolsRenderer);
<% });
...
下面名为ToolsRenderer的网格模型方法用于渲染我的html字符串。
public UserManagementViewModel : BaseModel {
//..snip
//
public object ToolsRenderer(Client client)
{
List<string> links = new List<string>();
var editLink = new TagBuilder("a");
// here is my biggest problem, before Html.ActionLink used to make
// sure that I don't have any missing links or help me if i need to
// refactor an action / controller name :-(
editLink.Attributes["href"] = GetEditUserLink(client, HttpContext.Current.Request.Url.AbsoluteUri);
editLink.SetInnerText("edit");
links.Add(editLink.ToString());
...
...lots of links to be generated here
...
return MvcHtmlString.Create(string.join(" |", links))
}
//..snip
}
现在可以使用,但有没有办法让我的aspx页面像下面的剃刀视图一样工作?
@Html.Grid(Model).Columns(column =>
{
column.For(x => x.Id).
column.For(x => x.Name);
column.Custom(@<td><a href='@Html.Actionlink("edit","user",new {id})' alt="@item.email"/><a></td>)
})
我想说的是:
...
column.Custom(%><td><a href='<%=Html.Actionlink("edit","user",new {id})%>' alt="<%=item.email%>"/><a></td><%)
...
答案 0 :(得分:3)
最后设法找到了一些帮助和挖掘的工作,而不是使用自定义列,使用column.for并推入Html.Partial。类似下面的代码。
<% Html.Grid(Model).Columns(column => {
column.For(x => x.Id);
column.For(x => x.Name);
column.For(x => x.Email);
column.For(x => x.DateOfBirth);
// could use RenderPartial() to
column.For(x =>Html.Partial("UserActionColumn", x));
<% });
答案 1 :(得分:1)
对于Razor案例,@ helper可用于自定义字段。
例如:
column.For(r => Status(r.Status)).Encode(false).Named("Status");
@helper Status(string value)
{
if (value == RequestStatus.Pending)
{
<span class="label label-info">@value</span>
}
...
}
答案 2 :(得分:0)
补充其他答案,如果您想按该自定义列排序,请不要忘记添加SortColumnName
。如果您不这样做,网格将使用Named
参数作为列名称,但Named
也用于为列提供标题。我只是头疼,因为我的专栏名称与标题不匹配:
column.For(c => "custom text").Named("Column Name").SortColumnName("ColumnName");