webgrid is Easy to use ,in mvc3 but the parameter is too many, each page need to set the parameter is so so terrible, Is there any way to fixed a few parameter in Templates , for example to fix :
tableStyle: "webgrid-table"
, headerStyle: "webgrid-header"
, alternatingRowStyle: "alt"
, selectedRowStyle: ""
, displayHeader: true
, numericLinksCount: 10
, footerStyle: "paging-number"
, rowStyle: "webgrid-row"
and common columns delete,add,edit
grid.Column(format: (item) => Html.ActionLink(" " , "add" , null , new { @class = "webgrid-add", title = " add row", onclick = "return gridop(this);" })) , grid.Column(format: (item) => Html.ActionLink(" " , "edit" , null , new { id = item[0]} , new { @class = "webgrid-edit", title = "edit row", onclick = "return gridop(this);" })) , grid.Column(format: (item) => Html.ActionLink(" " , "del" , null , new { id = item[0]} , new { @class = "webgrid-del", title = " delete row", onclick = "return gridop(this);" }))
If completed before the code,When we are ready to show webgrid, only to coding " showgrid(Model); " in page;
gridmodel.cshtml
@model IEnumerable @{ var grid = new WebGrid(Model,rowsPerPage:10);
if (ViewData.TemplateInfo.TemplateDepth > 1)
{
@ViewData.ModelMetadata.SimpleDisplayText;
}
else
{
List<WebGridColumn> columns = new List<WebGridColumn>();
columns.Add(grid.Column(format: (item) => Html.ActionLink(" "
, "add"
, null
, new { @class = "webgrid-add", title = " 添加新记录!", onclick = "return gridop(this);" })));
columns.Add(grid.Column(format: (item) => Html.ActionLink(" "
, "edit"
, null
, new { @class = "webgrid-edit", title = " 编辑本行记录信息!", onclick = "return gridop(this);" })));
columns.Add(grid.Column(format: (item) => Html.Ac开发者_Go百科tionLink(" "
, "del"
, null
, new {id = item[1] }
, new { @class = "webgrid-del", title = " 删除本行记录信息!", onclick = "return gridop(this);" })));
if (ViewData.TemplateInfo.TemplateDepth > 1)
{
@ViewData.ModelMetadata.SimpleDisplayText;
}
else
{
foreach (var item in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit &&
!ViewData.TemplateInfo.Visited(pm)))
{
if (!item.HideSurroundingHtml)
{
columns.add(new WebGridColumn { ColumnName = item.PropertyName, Header = item.DisplayName });
}
}
}
var a = grid.GetHtml(tableStyle: "webgrid-table"
, headerStyle: "webgrid-header"
, alternatingRowStyle: "alt"
, selectedRowStyle: ""
, displayHeader: true
, caption: "aaaaaaaaaaaaaaaa"
, numericLinksCount: 10
, footerStyle: "paging-number"
, rowStyle: "webgrid-row"
, columns: grid.Columns(columns.ToArray()
)
);
@Html.Raw(a.ToString());
}
}
over is my code ,but ViewData.ModelMetadata.Properties is null, can't to retrieve item.PropertyName, item.DisplayName . I do not know why! Please help me !
Please try using optional, named parameters. I mean, define the same default values in your control. This control should be an extension method of HtmlHelper class. Thus you can make from your View(s):
@Html.MyGrid(Model)
I commonly create a /Helpers folder for that purpose.
Hope this helps,
精彩评论