在过去几天搜索之后,我正式陷入困境.我正在研究将对象绑定到Telerik MVC 3 Grid,但问题是它需要动态创建的列(不是自动生成的).其中三列是已知的,其他列是未知的,这是棘手的部分.基本上,它可以像这些例子:
KnownColumn1 | KnownColumn2 | UnknownColumn1 | KnownColumn3
KnownColumn1 | KnownColumn2 | UnknownColumn1 | UnknownColumn2 | UnknownColumn3 | KnownColumn3
等等
因为我将未知列放在列表中(我也尝试过字典,所以我可以获取列名),这在绑定时对我来说很复杂.我的代码如下:
模型(可以有零到几百行,但是这个模型在List类型的视图模型中,也可以有0到20个动态添加的列)
public class VendorPaymentsGLAccount { public string GeneralLedgerAccountNumber { get; set; } public string GeneralLedgerAccountName { get; set; } public string DisplayName { get { return string.Format("{0} - {1}",GeneralLedgerAccountNumber,GeneralLedgerAccountName); } } public Dictionary<string,double> MonthAmount { get; set; } public double Total { get { return MonthAmount.Sum(x => x.Value); } } public List<string> Columns { get; set; } public List<double> Amounts { get; set; } public VendorPaymentsGLAccount() { } }
查看(注释掉的部分试图使用字典)
<fieldset>
<legend>General Ledger Account Spend History</legend>
@if (Model.VendorPaymentsGLAccounts != null)
{<br />
@(Html.Telerik().Grid(Model.VendorPaymentsGLAccounts)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(gl => gl.DisplayName).Title("General Ledger Account").Width(200).Filterable(false).Sortable(false);//foreach (var month in Model.VendorPaymentsGLAccounts[0].MonthAmount)
//{
// //columns.Bound(gl => gl.MonthAmount[month.Key.ToString()].ToString()).Title(month.Key.ToString()).Width(100).Filterable(false).Sortable(false);
// //columns.Template(v => Html.ActionLink(v.VoucherID,"VoucherSummary",new { id = v.VoucherID,bu = v.BusinessUnitID,dtt = v.InvoiceDate.Ticks })).Title("Voucher").Width(100);
// columns.Template(gl => Html.ActionLink(gl.MonthAmount[month.Key.ToString()].ToString(),"VoucherSummary")).Title(month.Key.ToString()).Width(100);
/