在网络网格中添加行
我正在使用 MVC 3 webgrid,我需要在 webgrid 中添加一个新行以显示产品表中的价格总和。
任何想法表示赞赏。
这是我的代码
@{
WebGrid grid = new WebGrid(source: Model,
rowsPerPage: 3,
canSort: true,
canPage: true,
ajaxUpdateContainerId: "ajaxgrid");
@grid.GetHtml(
alternatingRowStyle: "altrow",
mode: WebGridPagerModes.All,
firstText: "<< ",
previousText: "< ",
nextText: " >",
lastText: " >>",
columns: grid.Columns(
grid.Column(format: (item) => new HtmlString(Html.ActionLink("Edit", "Edit", new { id = item.ProductId }).ToString() + " | " +
Html.ActionLink("Delete", "Delete", new { id = item.ProductId }).ToString()
)
),
grid.Column("ProductId", "Product Id"),
grid.Column("CategoryId", "Category Name", format: (item) => item.Category.CategoryName),
grid.Column("ProductName", "Product Name"),
grid.Column("Price", "Price", format: @<text>@String.Format("{0:c}", item.Price)</text>)
)
)
}
I'm working with MVC 3 webgrid and i need to add a new row into webgrid to show sum of price from product table.
Any ideas are appreciated.
Here's my code
@{
WebGrid grid = new WebGrid(source: Model,
rowsPerPage: 3,
canSort: true,
canPage: true,
ajaxUpdateContainerId: "ajaxgrid");
@grid.GetHtml(
alternatingRowStyle: "altrow",
mode: WebGridPagerModes.All,
firstText: "<< ",
previousText: "< ",
nextText: " >",
lastText: " >>",
columns: grid.Columns(
grid.Column(format: (item) => new HtmlString(Html.ActionLink("Edit", "Edit", new { id = item.ProductId }).ToString() + " | " +
Html.ActionLink("Delete", "Delete", new { id = item.ProductId }).ToString()
)
),
grid.Column("ProductId", "Product Id"),
grid.Column("CategoryId", "Category Name", format: (item) => item.Category.CategoryName),
grid.Column("ProductName", "Product Name"),
grid.Column("Price", "Price", format: @<text>@String.Format("{0:c}", item.Price)</text>)
)
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了类似的问题,因此我创建了内置 MVC WebGrid 的扩展。代码和示例用法位于 https://gist.github.com/3211605。希望它有帮助。
I had a similar problem so I created an extension of the built in MVC WebGrid. Code and example usage is at https://gist.github.com/3211605. Hopefully it's helpful.
如果您可以使用 JQuery 并且页脚内容不按列对齐,那么向 WebGrid 添加页脚有点麻烦,但并不困难:
基础知识是:
更健壮解决方案应该确保这些元素存在,但您明白了。
假设您的模型已经包含总和(或获取总和的方法),只需构建页脚,如下所示:
If you're okay with using JQuery and having the footer content not being column-aligned, adding a footer to a WebGrid is a bit hacky, but not difficult:
The basics are:
A more robust solution should make sure those elements exist, but you get the idea.
Assuming your model already contains the sum (or a method to get the sum), just build your footer like:
这是一个可怕的黑客,但它让我继续我的一天。我认为正确执行此操作的方法是编写一个 Html Helper,允许您传入页脚行。我对 WebGrid 没有内置页脚行感到有点失望。
只需确保与手工构建的表格行中的列数匹配即可。请注意,没有表开始标记。整个想法是劫持表关闭时创建的 html 并添加一行。
抱歉手写代码。我使用的是专有应用程序,我不敢粘贴其中的任何内容......
This is a terrible hack but it allowed me to get on with my day. I think the way to do this correctly is to write an Html Helper that allows you to pass in the footer row. I'm a little disappointed the WebGrid doesn't have a footer row built in.
Only make sure you match the number of columns in your hand built table row. Notice there is no table start tag.. The whole idea is to hijack the created html at the table close and add a row.
Sorry for the hand typed code. I'm on a proprietary app and I don't dare paste anything from it...
将新项目添加到 IEnumerable 中,用作要绑定到的 WebGrid 的源。
我还没有找到一种很好的方法来将格式应用于该总行。这可能必须在客户端完成。
Add a new item to the IEnumerable that you use as the source for the WebGrid to bind to.
I haven't found a nice way to apply formatting to this total row yet. That might have to be done client-side.