简体   繁体   English

Asp.net:通过模型绑定或通过数据源绑定ListView哪个更好?

[英]Asp.net: Which Better Bind ListView By Model Binding or By DataSource?

I build a asp.net Web Application Webforms with Entity Framework, I find Two Way to bind the ListView for: 1- By DataSource Like this 我使用实体框架构建了一个asp.net Web应用程序Webforms,我发现了两种方法绑定ListView的方法:1-通过数据源像这样

 void Bind()
    {
        var search = db.Search.Where(k => k.RequestId == RequestId);
        lstSearch.DataSource = search.ToList();
        lstSearch.DataBind();
    }

2- By use SelectMethod Like this 2-通过使用SelectMethod像这样

public IQueryable<Search> BindOrders()
{
    var search = db.Search.Where(k => k.RequestId == 12).AsQueryable();

    return search;
}

which one is best and why? 哪一个最好,为什么?

SelectMethod and many other features for binding data to web controls were introduced in .NET Framework 4.5 as a strongly-typed data-binding. .NET Framework 4.5中将SelectMethod和许多其他用于将数据绑定到Web控件的功能作为强类型的数据绑定引入。 Those features let's you handle the data you create/delete/modify/filter from/to web controls in a clean and maintainable way. 这些功能使您可以以干净且可维护的方式处理从Web控件创建/删除/修改/过滤的数据。

In the other hand DataSource way is the old way of binding data to web controls. 另一方面, DataSource方法是将数据绑定到Web控件的旧方法。

I advise you to read this blog which goes in detail about the subject (ScottGu's): 我建议您阅读博客,其中详细介绍了该主题(ScottGu's):

The new Model Binding support in ASP.NET vNext is a nice evolution of the existing Web Forms data-binding system. ASP.NET vNext中新的模型绑定支持是对现有Web Forms数据绑定系统的不错改进。 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. 它借鉴了ASP.NET MVC中的“模型绑定”系统的概念和功能(您将在以后的文章中看到更多信息),并使使用以代码为中心的数据访问范例的工作变得更简单,更灵活。

Also, check for the advantages of using IQueryable<Object> : 另外,检查使用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. 从用户的角度来看,主要区别在于,当您使用IQueryable(通过正确支持事物的提供程序)时,可以节省大量资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM