Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

ASP.NET 4.5 Web Forms Model Binding

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[Microsoft] [] [Visual Studio 2012 and One ASP.NET] [Scott Hanselman, Principal Community Architect] [Microsoft Corporation] Hi, this is Scott Hanselman, and I wanted to share with you some of the new improvements in Visual Studio 2012 and ASP.NET 4.5. [Model Binding] In this video, specifically model binding. The upcoming version of ASP.NET Web Forms has a number of improvements to data binding. The most significant of these, I think, is model binding. In previous examples you've seen grid views populated with data using the data source property and calling the data bind method from a code behind. And while these approaches work, they have some limitations that are hard to overcome. Let's take a look at another way. I'm going to pull some data into the categories grid view here. I've got a grid view, and this is a grid view you've probably used before in Web Forms, except I'm getting the data a little bit differently. Rather than using an object data source or using data source and data binding from the code behind, we've added a select method property. This tells the grid view a method to call to retrieve its data. Then in the code behind you can implement that method and return an IQueryable. In this case, SelectMethod will call GetCategories. Look at the code behind. GetCategories is going to go and return categories from, in this case, entity framework, although you can use any database technology that supports link, and that's it. Notice that there's no data binding in there. We're just going to return categories, an IQueryable of category, and then the columns, in this case, using a dynamic field. It's going to automatically figure out the right thing to do to show our grid. Notice also our URL doesn't say .aspx. We've got just /categories, not /categories.aspx. We're using routing to do that, and that's the same routing that ASP.NET MVC developers have enjoyed for a while. ASP.NET Web Forms developers can do that as well. It's important to remember as we're going to click on edit, notice as I hover over edit in the lower corner of the browser there it says /categories/2. It says it can't be found, and no matter what I type in up here it's going to fail. Let's go back and look at categories edit. Categories edit is going to call GetCategory. That's its select. Its grid is set up to call GetCategory. But where is that value from the URL going to come from? In this case, I've got a method that takes an int, a knowable int with an ID, and in the old days, you might say something like Request.QueryString and ask for ID, and then you should make sure that ID is not null or quote, quote, and you'd spend a lot of time digging around inside of the HTTP request. Well, I can actually decorate this method's parameter with things like QueryString. And that would pull ID out of the query string, or I could say form, or I could say cookie. In this case, because I'm using routing, I'm going to say route data. I'm going to pull ID out of the route. That ID right there is expressed in our route over here in the global ASAX. CategoriesEdit, Categories/id. When we say Categories/2 that ID is going to get pulled out automatically. And because it's named ID, that's going to get populated. Let's actually run a debugging session so we can see that happen live. Hover over, hit edit. Notice that the value of 2 has automatically been pulled out of the URL. I didn't have to say request.this or request.that. I just decorated it with the place that the data was located, and it handled it for me, so that parameter was found automatically. Then I'll hit F5, and we'll go back to our page, make a small change. In this case, we populated the edit category. Now we're going to update it. I'm going to hit save. And rather than thinking about things in terms of postbacks, notice that update category has been called. Take a look at my grid again. We saw that SelectMethod used to GetCategory, and now the update method uses UpdateCategory. The form view is automatically doing that for us. Where is category ID coming from? Well, in this case, it's coming from the data control itself. The data control knows that we're talking about categories, and we've said that the key for the type of object that we care about, the type of object is a category, the key for that category is CategoryID. When that postback occurred, we automatically populated CategoryID. But we need all the values that are in the text boxes. We need to say request.form and get IDs and descriptions and fill our objects with all sorts of information. Often this kind of code turns into what I call left-hand right-hand code where you're saying = request.form"id." No fun. Take a look at category. It's null right now. I'm going to pin that down and put it over here so we can look at it. I'm going to start stepping over. Notice that category just got filled up. It just got filled up with the current value of what's in category, specifically "lovely smocks and shirts." We can see the value of category description and category name, and you see I've pinned them there, because I want to see them change. Now, here's where the magic happens. We've got the information for the newly updated category living inside of the HTTP post. We've posted back information that we need to get out. We could spend time digging for it, which is boring, or we could use the information that we know about categories and line them up. We could call try update model. I'm going to hit step over and watch right here. Notice that my pinned debug watch window there turned red to show me what value changed. We automatically updated the object, even complicated objects, and since the state of that object is valid, we can step over that as well and then save the changes. Return to the previous page, and our value is updated. Model binding has a lot of flexibility. We've used it here for gets, for updates. I can decorate my parameters with things like query string, forms, cookies, route data. It makes for a much nicer looking Web Forms experience. And again, since ASP.NET 4.5 is an in-place upgrade of ASP.NET 4.0, your existing techniques for data access in ASP.NET will continue to work, but you can start introducing pages with these new techniques. You can start using the same repositories that your ASP.NET MVC fellow programmers are using. You can start using routing and then pull that information out of the route just like they can. And you're going to find that you're going to end up deleting a lot of code in your Web Forms. That's good. That's called refactoring by deletion. That means you're going to write more code that does work and less code that digs around inside of the HTTP request. I hope you like these new features of ASP.NET Web Forms. I encourage you to check out the other videos. [Microsoft] []

Video Details

Duration: 7 minutes and 50 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 5
Posted by: neudesicasp on Oct 3, 2013

ASP.NET 4.5 Web Forms Model Binding

Caption and Translate

    Sign In/Register for Dotsub to translate this video.