在Controller中以编程方式创建WebGrid列? [英] Programmatically creating WebGrid columns in the Controller?

查看:107
本文介绍了在Controller中以编程方式创建WebGrid列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不知道如何在控制器中动态设置列集合。我的控制器中有示例代码:



 public ActionResult GenericGrid()
{
var books = InitVars();


WebGrid grid = new WebGrid(books,ajaxUpdateContainerId:grid,ajaxUpdateCallback:setArrows);

WebGridColumn col1 = grid.Column(BookId,,(item)=> GetEditButtons(item),col1,true);
// ... 2-6也初始化...

列表< WebGridColumn> columnSet = new List< WebGridColumn>(){col1,col2,col3,col4,col5,col6};
grid.Columns(columnSet.ToArray());

ViewBag.Grid = grid;

return View();
}

公共对象GetEditButtons(动态项)
{
int bookID = item.BookId;

返回< button class ='delete-book display-mode'id ='+ bookID +'>删除< / button> +
< button class ='edit-book display-mode'id ='+ bookID +'>编辑< / button> +
< button class ='save-book edit-mode edit-width'id ='+ bookID +'> Save< / button>;
}





我尝试在视图中使用它:

< div id =grid> 
@ grid.GetHtml(
tableStyle:table,
alternatingRowStyle:alternate,
selectedRowStyle:selected,
headerStyle:header





我得到的是一个包含所有字段的网格(以及BookId作为标题和书籍ID为字段。)



如果我在视图中直接执行此操作:



 @ grid.GetHtml(
tableStyle:table,
alternatingRowStyle:alternate,
selectedRowStyle:selected,
headerStyle:header,
列:grid.Columns(
grid.Column(,
style:col1,
格式:@< text>
< button class = delete-book display-modeid =@ item.BookId>删除< / button>
< button class =edit-book display-modeid =@ item.BookId> Edit< ; / button>
< button class =save-book edit-mode edit-widthid =@ item.BookId> Save< / button>
< / text>)) )





lo看哪,它有效。



但我不想那样做!我想在控制器中动态创建列。



我注意到的一件事是从未调用 GetEditButtons ,即使我在控制器中调用grid.GetHtml()作为测试。



想法?



Marc

解决方案

这里有一个时间问题......你可以做一些工作但我不得不问,为什么?

为什么不在视图中放置格式?你想使用具有相同视图的动态模型吗?

无论如何 - 即使使用我的解决方案,你仍然会遇到html内容GetEditButtons返回的问题 - 这个内容将被编码并显示为而不是真正的HTML ......

列表< webgridcolumn> columnSet =  new 列表< webgridcolumn>(){col1,col2,col3,col4,col5,col6}; 
ViewBag.GridCols = columnSet; < / webgridcolumn > < / webgridcolumn >



 @ grid.GetHtml(
tableStyle: table
alternatingRowStyle: alternate
selectedRowStyle: selected
headerStyle: header
columns:ViewBag.GridCols



它会调用你的方法,但它返回的html内容不会创建真正的html,但会被编码......

如果你坚持在控制器中创建列你最好使用扩展功能而不是GetHtml并在那里进行更改

http://stackoverflow.com/questions/11698665/mvc3-web-grid-adding-action-links-at-the-begining-of-columns-list [ ^ ]

最好使用ActionLink帮助方法创建可点击链接(如编辑/删除),而不是像你那样创建HTML内容...


I'm just not getting it how to set columns collection dynamically in the controller. I have this sample code in my controller:

public ActionResult GenericGrid()
{
  var books = InitVars();


  WebGrid grid = new WebGrid(books, ajaxUpdateContainerId: "grid", ajaxUpdateCallback: "setArrows");
			
  WebGridColumn col1 = grid.Column("BookId", "", (item) => GetEditButtons(item), "col1", true);
  // ... 2-6 initialized also ...

  List<WebGridColumn> columnSet = new List<WebGridColumn>() {col1, col2, col3, col4, col5, col6};
  grid.Columns(columnSet.ToArray());

  ViewBag.Grid = grid;

  return View();
}

public object GetEditButtons(dynamic item)
{
  int bookID = item.BookId;

  return "<button class='delete-book display-mode' id='"+bookID+"'>Delete</button>" +
"<button class='edit-book display-mode' id='"+bookID+"'>Edit</button>" +
"<button class='save-book edit-mode edit-width' id='"+bookID+"'>Save</button>";
}



And I try to use it like this in the view:

<div id="grid">
  @grid.GetHtml(
    tableStyle : "table",
    alternatingRowStyle : "alternate",
    selectedRowStyle: "selected",
    headerStyle : "header"
    ) 
  )



All I get is a grid with all the fields (and BookId as the header and the book ID's as the fields.)

If I do this directly in the view:

@grid.GetHtml(
  tableStyle : "table",
  alternatingRowStyle : "alternate",
  selectedRowStyle: "selected",
  headerStyle : "header",
  columns : grid.Columns(
  grid.Column("", 
  style: "col1", 
  format: @<text>
    <button class="delete-book display-mode" id="@item.BookId">Delete</button>
    <button class="edit-book display-mode" id="@item.BookId">Edit</button>
    <button class="save-book edit-mode edit-width" id="@item.BookId">Save</button>
</text>)))
) 



lo and behold, it works.

But I don't want to do that!!! I want to create the columns dynamically in the controller.

One thing I note is that GetEditButtons is never called, even if I call grid.GetHtml() as a test in the controller.

Ideas?

Marc

解决方案

There is a timing issue here...You can do some work around but I have to ask, why?
Why not to put the formatting int the view? are you want to use dynamic models with the same view?
In any case - even with my solution you still will have the problem of the html content GetEditButtons returns - this content will be encoded and displayed as such and not as real html...

List<webgridcolumn> columnSet = new List<webgridcolumn>() {col1, col2, col3, col4, col5, col6};
ViewBag.GridCols = columnSet;</webgridcolumn></webgridcolumn>


@grid.GetHtml(
  tableStyle : "table",
  alternatingRowStyle : "alternate",
  selectedRowStyle: "selected",
  headerStyle : "header",
  columns : ViewBag.GridCols
)


It will call your method, but the html content returned by it will not create real html, but will be encoded...
If you insist on creating column in the controller you better make an extension function instead of GetHtml and make you changes there
http://stackoverflow.com/questions/11698665/mvc3-web-grid-adding-action-links-at-the-begining-of-columns-list[^]
It is also better to use ActionLink helper method to create clickable links (like edit/delete) instead of creating html content like you did...


这篇关于在Controller中以编程方式创建WebGrid列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆