我GridView
在 ASP.NET MVC 4 项目中使用 DevExpress 扩展 v12.2。
GridView
'sDataSource
设置为一个对象,该System.Data.DataTable
对象是通过在 SQL Server 2008 R2 数据库上运行自定义 SQL 查询获得的。
我绑定到 a 的原因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!");
}
请帮忙。我应该将数据绑定到什么对象?