ObjectDataSource - 您的加载方法可以驻留在与实体不同的类中吗?
我有以下类模型:
public class Person
{
public string Name;
public int Age;
}
public class PersonService
{
public List<Person> GetAll() {...}
}
我通过将 ObjectDataSource (ODS) 绑定到 GridView 在 ASP.Net 网页上显示数据。
如果我指向“ODS.TypeName = PersonService”,那么它会给出“对象与目标类型不匹配”。加载数据时出错。
如果我指向“ODS.TypeName = Person”,那么它找不到 GetAll() 方法来加载数据。
是否可以将 ODS 绑定到此模型(即方法和类型的单独类)?
编辑:我已经仔细检查了 Type 和 Select 方法名称是否正确(并且完全限定)。我做了一个单独的快速测试项目来证明 ODS 适用于上述模型 - 确实如此。现在唯一的区别是损坏的项目正在使用 EF 4.1 定义的实体 - 这会导致问题吗?
I have the following class model:
public class Person
{
public string Name;
public int Age;
}
public class PersonService
{
public List<Person> GetAll() {...}
}
I'm displaying the data on an ASP.Net web page by binding an ObjectDataSource (ODS) to a GridView.
If I point the 'ODS.TypeName = PersonService' then it gives an "Object does not match target type." error on loading data.
If I point the 'ODS.TypeName = Person' then it can't find the GetAll() method to load data.
Is it possible to bind the ODS to this model (i.e. separate classes for the method and type)?
Edit: I've double checked Type and Select method names are correct (and fully qualified). I made a separate quick test project to prove ODS works with the above model - it does. The only difference now, is that the broken project is using an Entity defined by EF 4.1 - would that cause a problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的示例中,
TypeName
需要设置为PersonService
。无需向 ODS 提供绑定到网格行的确切对象类型(示例中为Person
),因为 ASP.NET 数据绑定和 ODS/网格都不会真正关心。关于您的错误,请尝试完全限定服务,例如My.Namespace.PersonService
,并确保在 ODS 上相应地设置SelectMethod
。TypeName
needs to be set toPersonService
in your example. There is no need to provide the ODS with the exact type of object bound to the grid's rows (Person
in your example), as neither ASP.NET data binding nor the ODS/grid will really care. Concerning your error, try to fully qualify the service, e.g.,My.Namespace.PersonService
, and make sure to setSelectMethod
on the ODS accordingly.我认为您想要创建一个 PersonList 类,它公开 Person 对象的列表,并且它有一个方法使用 PersonService 来填充/返回列表。
I think you want to create a PersonList class that exposes a list of Person object and which has a method uses PersonService to populate/return a list.
在此处输入图像描述
只需右键单击您的项目并选择“禁用轻量级解决方案加载”
它会起作用的。没有其他问题
enter image description here
Just right click on your project and select "Disable Lightweight Solution Load"
it will work. there is no other issue