My progress with ASP.NET MVC is considered mentionable. My personal project is
Songhay.Web.Mvc.ServerIndex—which is the index of my development server. This project helps to get a foothold on current Microsoft technologies within my scope—and it functions in the same manner as a SharePoint “My Site” (without SharePoint).
ASP.NET forms and MVC
You really, really can’t use ASP.NET forms (well) with ASP.NET MVC. The problem starts when you assume that
ViewState will be properly maintained for, say,
GridView paging. Some developers find the
ListView helpful—others attempt to hack
ViewState with the help of an online tool like KeyCreator.
The problem continues when you get used to using inline code blocks (
<% %>) in views—and you realize (again) that ASP.NET server controls do not support code blocks very well.
|“ASP.NET MVC doesn’t work with ViewState and Postback?”||“ASP.NET’s server-side controls work with WebForms, not MVC. MVC doesn’t use controls in the traditional ASP.NET sense (at least yet).”|
|“Is ViewState relevant in ASP.NET MVC?”||“[It’s] present because
|“ASP.NET MVC Validation of viewstate MAC failed error”||I do not recommend seeing the contents of this article as a solution because it opens a security hole in your application.|
MVC Model and View
The presence of the Model is made explicit when it is passed as a Generic Type argument when declaring inheritance for a
<%@ Import Namespace="Songhay.Web.Mvc.ServerIndex.Models" %> <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyViewData>" %>
When a “view data” object is passed to a view, the Model object will now have the properties of this object. This means that you can use
<%=Model.MyViewDataProperty%> instead of something like
"]%>—or that old
Eval syntax Scott Guthrie was showing (with strongly-typed View data) back in 2007. This ‘new’ (alternative) Model syntax is really cool and is the first time I have seen so explicit an expression of Model and View in an MVC framework! Here is a quote from Microsoft:
You can put views into one of two categories: those that use the
ViewDatadictionary and those that use strong typing. Views that use the
ViewDatadictionary derive from
System.Web.Mvc.ViewPage. Our tag soup example uses this approach. Strongly typed view pages derive from
Tis a generic parameter to specify the type of the model. I recommend you use strong typing.
There is no longer a need to “register” ASCX files because of the
Html.RenderPartial() helper. Jeffery Palermo insists that you can get rid of ASCX files entirely but I’m not seeing that being easy in my current version of Visual Studio 2008.
|“ASP.NET MVC and the templated partial view (death to ASCX)”||Simply, Jeff’s
|“Guiding Principles For Your ASP.NET MVC Applications”||This Scott Allen article is very important. It should actually be printed for easy reference.|
|“ASP.NET MVC Framework (Part 3): Passing ViewData from Controllers to Views”||
“In addition to supporting a late-bound dictionary approach, the ASP.NET MVC Framework also enables you to pass strongly typed ViewData objects from your Controller to your View. There are a couple of benefits of using this strongly typed approach:”
“1. You avoid using strings to lookup objects, and get compile-time checking of both your Controller and View code”
“2. You avoid the need to explicitly cast values from the ViewData object dictionary when using strongly-typed languages like C#”
“3. You get automatic code intellisense against your ViewData object within both the markup and code-behind of your view page”
“4. You can use code refactoring tools to help automate changes across your app and unit-test code base”
|“ASP.NET MVC Tip #31—Passing Data to Master Pages and User Controls”||“I’m not completely convinced by my own tip. I’m still tempted to use action filters to add view data for my master pages and user controls. The solution described In the last section, using abstract base classes, seems like a lot of work. I’m curious about how others have solved this problem.”|
|“Typed View Data and Master Pages”||“The ASP.NET MVC framework supports typed view data through the
MVC Model Definitions and “View Data” Definitions
A key concept for me is containing the Model in two logical areas: Model Definitions and “View Data” Definitions. We should all know by now what Model Definitions are (the data-system-centric Model in MVC). What is new to me is the “View Data” definition—these define what will be sent as strongly typed data to the View. A previous post records how I divide the data layer conceptually: Procedures, Data Transfer Contracts, Input Definitions and Output Definitions—here, in ASP.NET MVC land, these “View Data” Definitions are my Output Definitions. With this single connection in place, I can see how MVC can get to the data—all the rest of the stuff falls into place (as of today). Now that I’ here, let me write these:
- Input Definitions use Data Transfer Contracts to couple with Procedures for data input.
- Output Definitions use Data Transfer Contracts to couple with Procedures for data output.
- Data procedures are loosely coupled from input and output concerns because of Data Transfer Contracts.
I’ve never written (or spoken) these lines before. So it’s time to get started!
|“Collection<T> versus List<T> what should you use on your interfaces?”||“…by offering a simpler collection class or interface, you reduce the number of members that users of your API see, and make it easier for them to use.” This subject is important for ASP.NET MVC because a View will need to iterate over its strongly-typed “rows” of data. The recommended data type for these rows is
|“What is the difference between ICollection and IDictionary in .NET/C#?”||“Finally, note that these are all interfaces, not classes. .NET gives you a few different types of collections, lists, and dictionaries to choose from, and the interfaces like
By no means do I “know everything” about ASP.NET MVC. The following table summarizes areas needing more investigation and study:
|“ASP.NET MVC Preview 5 and Form Posting Scenarios”||
Posted a little over a year ago, Scott Guthrie writes about the importance of
Then he goes on to Model Binders, “Model Binders provide a way for complex types to be de-serialized from the incoming HTTP input, and passed to a Controller action method as arguments. They also provide support for handling input exceptions, and make it easier to redisplay forms when errors occur (without requiring the end-user to have to re-enter all their data again—more on this later in this blog post).”
Other topics of interest are:
|“VS 2008 Nested Master Page Support”||“One of the sites I recommend checking out is http://www.opensourcetemplates.org/. This is an online repository of free HTML site templates that you can browse, download and use.”|
|“ASP.NET MVC Tip #1—Create New HTML Helpers with Extension Methods”||
“You can add new functionality to the
By looking at source code from big shots, I can see that this technique is encouraged for
|“Authenticating Users with Windows Authentication (C#)”||“After you enable Windows authentication, you can use the
|“ASP.NET MVC Best Practices (Part 1)”||This Kazi Manzur aricle makes me miss a full-duplex laser printer! This is another one that deserves paper.|