我在我的asp mvc模型中向jqgrid中的工具栏添加了一个自定义按钮,但是每次从视图到控制器(OnDataRequested)调用获取数据时,都会再次添加该按钮。这意味着如果五次调用获取数据,则会在jqgrid上生成五个单独的按钮。我做错了什么?
这是我的代码。感谢。
public virtual ActionResult Index()
{
var gridModel = new LibraryGridModel();
SetupGrid(gridModel.LibraryGrid);
return View(gridModel);
}
public virtual JsonResult OnDataRequested()
{
var gridModel = new LibraryGridModel();
return gridModel.LibraryGrid.DataBind(GetFullLibrary().AsQueryable());
}
public void SetupGrid(JQGrid grid)
{
grid.DataUrl = Url.Action("OnDataRequested","Library");
grid.MultiSelect = true;
grid.MultiSelectMode = MultiSelectMode.SelectOnCheckBoxClickOnly;
}
Grid grid = new {ToolBarSettings = {ShowRefreshButton = true,ShowSearchButton = true,ShowSearchToolBar = true, ToolBarAlign = ToolBarAlign.Left,CustomButtons = new List { 新的JQGridToolBarButton { 文字=“添加到图书馆”, ToolTip =“将选定的会话添加到我的”, ButtonIcon =“ui-icon-plusthick”, Position = ToolBarButtonPosition.Last, OnClick =“AddMultipleSessionsToLibrary”, } }},
我正在我的LibraryGrid类中添加自定义按钮。谢谢你的帮助。
答案 0 :(得分:0)
首先,从GridModel中删除自定义按钮。然后,仅在索引操作调用中添加它 型号:
Grid grid = new{ ToolBarSettings = { ShowRefreshButton = true, ShowSearchButton = true, ShowSearchToolBar = true, ToolBarAlign = ToolBarAlign.Left }},
控制器:
public virtual ActionResult Index()
{
var gridModel = new LibraryGridModel();
gridModel.ToolBarSettings.CustomButtons.Add(
new JQGridToolBarButton
{
Text = "Add to Library",
ToolTip = "Add the selected session to mine",
ButtonIcon = "ui-icon-plusthick",
Position = ToolBarButtonPosition.Last,
OnClick = "AddMultipleSessionsToLibrary"
}
);
SetupGrid(gridModel.LibraryGrid);
return View(gridModel);
}
public virtual JsonResult OnDataRequested()
{
var gridModel = new LibraryGridModel();
return gridModel.LibraryGrid.DataBind(GetFullLibrary().AsQueryable());
}
public void SetupGrid(JQGrid grid)
{
grid.DataUrl = Url.Action("OnDataRequested","Library");
grid.MultiSelect = true;
grid.MultiSelectMode = MultiSelectMode.SelectOnCheckBoxClickOnly;
}