Asp.net:哪个更好地通过模型绑定或通过数据源绑定 ListView?
Asp.net: Which Better Bind ListView By Model Binding or By DataSource?
我用 Entity Framework 构建了一个 asp.net Web 应用程序 Webforms,我找到了两种绑定 ListView 的方法:
1- 通过 DataSource 这样
void Bind()
{
var search = db.Search.Where(k => k.RequestId == RequestId);
lstSearch.DataSource = search.ToList();
lstSearch.DataBind();
}
2- 通过像这样使用 SelectMethod
public IQueryable<Search> BindOrders()
{
var search = db.Search.Where(k => k.RequestId == 12).AsQueryable();
return search;
}
哪个最好,为什么?
SelectMethod
和许多其他用于将数据绑定到 Web 控件的功能在 .NET Framework 4.5
中作为强类型数据绑定引入。这些功能让您能够以干净且可维护的方式处理 create/delete/modify/filter from/to Web 控制的数据。
另一方面,DataSource
方法是将数据绑定到 Web 控件的旧方法。
我建议您阅读 this 博客,其中详细介绍了该主题(ScottGu 的):
The new Model Binding support in ASP.NET vNext is a nice evolution of
the existing Web Forms data-binding system. It borrows concepts and
features from the Model Binding system in ASP.NET MVC (you'll see this
more in later posts), and makes working with code-focused data-access
paradigms simpler and more flexible.
此外,检查 advantages 使用 IQueryable<Object>
:
The main difference, from a user's perspective, is that, when you use
IQueryable (with a provider that supports things correctly), you
can save a lot of resources.
我用 Entity Framework 构建了一个 asp.net Web 应用程序 Webforms,我找到了两种绑定 ListView 的方法: 1- 通过 DataSource 这样
void Bind()
{
var search = db.Search.Where(k => k.RequestId == RequestId);
lstSearch.DataSource = search.ToList();
lstSearch.DataBind();
}
2- 通过像这样使用 SelectMethod
public IQueryable<Search> BindOrders()
{
var search = db.Search.Where(k => k.RequestId == 12).AsQueryable();
return search;
}
哪个最好,为什么?
SelectMethod
和许多其他用于将数据绑定到 Web 控件的功能在 .NET Framework 4.5
中作为强类型数据绑定引入。这些功能让您能够以干净且可维护的方式处理 create/delete/modify/filter from/to Web 控制的数据。
另一方面,DataSource
方法是将数据绑定到 Web 控件的旧方法。
我建议您阅读 this 博客,其中详细介绍了该主题(ScottGu 的):
The new Model Binding support in ASP.NET vNext is a nice evolution of the existing Web Forms data-binding system. It borrows concepts and features from the Model Binding system in ASP.NET MVC (you'll see this more in later posts), and makes working with code-focused data-access paradigms simpler and more flexible.
此外,检查 advantages 使用 IQueryable<Object>
:
The main difference, from a user's perspective, is that, when you use IQueryable (with a provider that supports things correctly), you can save a lot of resources.