Thanks to everyone for coming to the phillydotnet presentation tonight. There were a lot of great questions, but a few recurring themes. The big one I noticed had to do with dealing with the UI, and how much we're going to sacrifice in leaving the WebForms world.
In the example we covered during the presentation, I showed how to bind basically everything in the .aspx page. For those that were put off by how 'Classic ASP' that looked, I've created another example that may look a little more familiar. Below, you're looking at the code for a page that that will show all of the products in the site using a repeater and a user control for each product.
<%@ Register Src="~/Views/Shared/ProductViewer.ascx" TagPrefix="eg" TagName="ProductViewer" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<asp:Repeater ID="rptProducts" runat="server">
<ItemTemplate>
<%# Html.RenderUserControl("~/Views/Shared/ProductViewer.ascx", Container.DataItem) %>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
And the codebehind:
public partial class Index : ViewPage<List<Model.Product>>
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
rptProducts.DataSource = ViewData;
rptProducts.DataBind();
}
}
As it turns out, there is a special MVCUser control you can add to your project which gives you access to the same ViewData available to the page. If you want access to just one portion of your ViewData (eg: If you used a Dictionary of values to pass data from your Controller to your view), you could further specify the ViewDataKey, which is simply the key of the item in the Dictionary you wanted the UserControl to bind. Finally, if you're doing something like what I am doing above (needing to bind to an instance in a collection, you can use the Html.RenderUserControl helper method, which allows you to bind whatever data you want. There's a good read on this here: Matt Hidinger: ASP.NET MVC UserControls Start to Finish.
Bear in mind that you could just create a regular, non-MVC usercontrol too. You would just create whatever properties you wanted to bind to in that UserControl, and set them in the containing CodeBehind. WARNING: Doing this means you have to again be aware of the PageLifecycle, because your user control can't try to bind its data until that data has been set.
