简体   繁体   中英

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

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

2- By use SelectMethod Like this

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. Those features let's you handle the data you create/delete/modify/filter from/to web controls in a clean and maintainable way.

In the other hand DataSource way is the old way of binding data to web controls.

I advise you to read this blog which goes in detail about the subject (ScottGu's):

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.

Also, check for the advantages of using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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