我使用 asp.net 和 c#。
我有一个 DetailsView,我需要获取绑定(bind)到它的数据。
我收到此错误:您知道如何解决吗? 感谢您的宝贵时间。
请提供代码示例。
// Get the data for underlying DetailsView
DetailsView myDetailsView = ((DetailsView)sender);
WebProject.DataAccess.DatabaseModels.CmsContent rowView (WebProject.DataAccess.DatabaseModels.CmsContent)myDetailsView.DataItem;
Unable to cast object of type 'System.Web.UI.WebControls.EntityDataSourceWrapper' to type 'WebProject.DataAccess.DatabaseModels.CmsContent'.
最佳答案
要从 DataItem 获取对象,您需要使用此处描述的方法:http://blogs.msdn.com/b/diego/archive/2008/05/13/entitydatasource-to-wrap-or-not-to-wrap.aspx 即创建类
static class EntityDataSourceExtensions
{
public static TEntity GetItemObject<TEntity>(object dataItem) where TEntity : class
{
var entity = dataItem as TEntity;
if (entity != null)
{
return entity;
}
var td = dataItem as ICustomTypeDescriptor;
if (td != null)
{
return (TEntity)td.GetPropertyOwner(null);
}
return null;
}
}
然后调用
var row = EntityDataSourceExtensions.GetItemObject<WebProject.DataAccess.DatabaseModels.CmsContent>(myDetailsView.DataItem)
关于c# - 无法将类型为 'System.Web.UI.WebControls.EntityDataSourceWrapper' 的对象转换为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6502774/