当 DevExpress MVC GridView 扩展绑定到 System.Data.DataTable 对象时,发布

本文关键字:DataTable Data 对象 发布 System MVC DevExpress GridView 扩展 绑定 | 更新日期: 2023-09-27 18:31:26

我在 ASP.NET MVC 4项目中使用DevExpress GridView扩展v12.2。

GridViewDataSource设置为 System.Data.DataTable 对象,该对象是通过在 SQL Server 2008 R2 数据库上运行自定义 SQL 查询来获取的。

我绑定到DataTable的原因是,运行 SQL 查询的结果每次都会有可变数量的列,因此结果的架构将是流畅的。因此,我无法绑定到特定的 ViewModel 或IEnumerable<MyPOCO>,因为为此我必须事先有一个固定的结构。

我抬起头,令人惊讶的是,DataTable不是IEnumerable.这是一个IListSource.但我离题了。

我使用以下代码来处理行编辑、删除和新行创建:

@Html.DevExpress().GridView(settings =>
    {
    settings.Name = "gvStrings";
    settings.CallbackRouteValues = new { Controller = "Strings", Action = "StringsPartial" };
    // Paging related stuff: not shown here 
    // because it's not relevant to the question
    foreach (DataColumn column in Model.Columns)
    {
        if ((string.Compare(column.ColumnName, "Foo", true) == 0) ||
            (string.Compare(column.ColumnName, "Bar", true) == 0))
        {
            continue;
        }
        else if (string.Compare(column.ColumnName, "GarId", true) == 0)
        {
            var categoryColumn = settings.Columns.Add(column.ColumnName, "Gar");
            // and so on...
        }
        else
        {
            settings.Columns.Add(column.ColumnName);
        }
    }
    settings.KeyFieldName = "FooId";
    settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Foo", Action = "CreateNew" };
    settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Foo", Action = "Edit" };
    settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Foo", Action = "Delete" };
    settings.SettingsEditing.Mode = GridViewEditingMode.Inline;
    settings.SettingsBehavior.ConfirmDelete = true;
    settings.CommandColumn.Visible = true;
    settings.CommandColumn.NewButton.Visible = true;
    settings.CommandColumn.EditButton.Visible = true;
    settings.CommandColumn.UpdateButton.Visible = true;
    settings.CommandColumn.DeleteButton.Visible = true;
}).Bind(Model).GetHtml()

我无法确定从客户端发送的数据在服务器上绑定到哪种类型的对象。

我尝试像这样绑定到System.Data.DataRow,但模型绑定器无法绑定到DataRow对象,因为DataRow没有无参数的默认构造函数。

[HttpPost]
public ActionResult CreateNew(DataRow row)
{
    System.Diagnostics.Debugger.Break();
    // The model binder can't bind to System.Data.DataRow because
    // DataRow does not have a parameterless, default ctor, so 
    // I receive an exception that says something like, 
    // "No parameterless default constructor was found on this object."
    return Content("Hello, there!");
}

然后,我尝试在签名中使用 System.object 并调用其 GetType() 来返回运行时类型,但它仅作为运行时类型返回System.object。请参阅下面的代码:

[HttpPost]
public ActionResult CreateNew(object row)
{
    System.Diagnostics.Debugger.Break();
    System.Diagnostics.Debug.Print(row.GetType().ToString());
    return Content("Hello, there!");
}

然后,我尝试像这样绑定到dynamic对象,但我无法反映它的属性。

[HttpPost]
public ActionResult CreateNew(dynamic row)
{
    System.Diagnostics.Debugger.Break();
    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(row))
    {
        System.Diagnostics.Debug.Print(prop.Name);
    }
    return Content("Hello, there!");
}

请帮忙。我应该将数据绑定到哪个对象?

当 DevExpress MVC GridView 扩展绑定到 System.Data.DataTable 对象时,发布

检查 DevExpress KB 中的 GridView Edit Value with DataTable Model 线程。