Practical ASP.NET

ASP.NET Processing in ASP.NET MVC

Peter Vogel wraps up his series on ASP.NET MVC, for now, by mapping some typical ASP.NET tasks to ASP.NET MVC.

In this series on "ASP.NET MVC for the ASP.NET Developer," I've been looking at ASP.NET MVC on its own terms. However, ASP.NET developers have to be asking how to perform typical tasks in this new environment: how to access data that the user enters into the page, how to respond to the user's actions in the browser, and so on. Some of these overlapping tasks require no explanation: ASP.NET MVC pages can use Master Pages, for instance, and the Session and Cache objects are as available from your server-side code in ASP.NET MVC as they are in ASP.NET.

In ASP.NET MVC, as in ASP.NET MVC, your HTML still includes a form element with at least one submit button. When generating HTML in ASP.NET MVC, however, you don't drag a control onto the page. Instead, you type in an "HTML Helper" that will generate your HTML at runtime. This code, for instance, generates an input tag of type text with its id a<p>nd name attribute set to "fname":</p>mm

<%= Html.TextBox("fname")%>

When you lay out the form element you'll specify a URL to ensure the right Controller and View is called when the user clicks the submit button. Using the HTML helpers you can ensure that the URL used when the submit button is clicked follows the format that your router will use to call the right controller. The major difference here is that control over the HTML generated is managed through the parameters passed to the method rather than through properties on a control.

Some things are completely different in ASP.NET MVC. For instance, you don't use IsPostBack to determine if a page is being displayed to the user for the first time. Instead, you specify which method in a controller is called when a page is first requested by adding an attribute to the method. This method, for instance, will be called when a page is first requested because it has the HttpGet attribute applied to it:

<HttpGet()> _
Function Create() As ActionResult
          Return View("NewPage")
End Function   

Access to the data on the page is handled differently. The method called when the controller is invoked on a page's postback can be configured to be passed an object with properties for each of the controls on the page. This example, called when a page postbacks, as specified by the HttpPost attribute, accepts a MyUser object that will hold the data from the page:

 <HttpPost()> _
 Function Create(ByVal user As MyUser) As ActionResult
   If user.UserName = "Vogel" Then

What you won't get in ASP.NET MVC is the event model you get with ASP.NET. If you want to determine if the user changed the value in a dropdownlist, you can't put code in the SelectedIndexChanged event. Before moving to ASP.NET MVC from ASP.NET, take a look at your existing applications and determine how often you use client-side events to determine what the user did in the browser.

You also won't get the dynamically generated HTML that ASP.NET controls provide. If you like what the GridView does for you, for instance, and don't want to responsible for doing that yourself then you'll probably be happier with ASP.NET than ASP.NET MVC.

That raises some interesting questions which I'll discuss in my next column.

Read the other columns in this series on ASP.NET MVC development:

ASP.NET MVC for the ASP.NET Programmer

Controlling Controllers in ASP.NET MVC

Viewing Views in ASP.NET MVC

Managing Models in ASP.NET MVC

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube