Simple clear advice in plain English

Delve into ASP.Net MVC

Microsoft’s new web framework goes under the microscope

Populating a view
How do you get dynamic data into a view?

The normal approach is to use the ViewData property, which is a dictionary of values available to both the controller and the view.

For example, perhaps an ID of 4 pulls up a product called Super Widgets.

Change the Index() method to accept an ID argument of type int, using the ‘?’ suffix to indicate that the value may be null:

public ActionResult Index 4 (int? id)
Then add some code:
if (id == 4)
{
ViewData[“product”] = “Widget”;
}

Now add some code to index.aspx:

The product you ordered is: <%= ViewData[“product”] % >.

Run the project, and navigate to the URL: http://[yourhost]/home/ index/4.

The value Super Widget is inserted in the page.

What about the Model?

The code to look up a product doesn’t belong in the controller.

The controller should have minimal logic, so you would normally define classes in the Models folder that handle this.

In ASP.Net MVC, the Model aspect is loosely defined.

Creating an instant application
In general, ASP.Net MVC is more manual and demanding than Web Forms.

That said, it has productivity features that let you snap together an application quickly.

To see this working, add a Widget class to the Models folder.

Give it some properties including at least an ID field to represent a p rimary key.

Compile the application, then add a Widget controller to the project.

This time, check the box for adding action methods.

Next, open WidgetController.cs and right-click the Index ActionResult.

Choose Add View, then check Create a Strongly Typed View.

Select HandsOnMVC.Models.Widget (the first part will vary according to the namespace of your project).

Select a View Content of List.

Do the same for the Edit, Create and Details views, selecting View Content of Edit, Create and Details.

At this point you almost have a working application. You do have to write some code to fill in the stubs.

For example, I created a WidgetManager in the Models folder that has a static GetWidgets method, to return a list of widgets.

Then I added this code to the Index method in WidgetController:

ViewData.Model = HandsOnMVC.Models.WidgetManager.GetWidgets();

The Model property is used by strongly-typed views to hold an instance of the data for the view.

Run the code, navigate to http://[yourhost] /Widget, and you get a list of widgets.

Check out the form for creating a new widget, noting that the URL is a Rest type: http://[yourhost]/ Widget/Create.

You need to edit the overloaded Create method that takes a FormCollection argument in WidgetController, to read the form values and save them to a database.

Going on with ASP.Net MVC
ASP.Net MVC is very different from the old Web Forms.

In some ways it looks more like the old ASP, with its variables embedded in HTML, and again you have to take care not to send user input (such as a string ID value) direct to the output as that is a security hole.

There’s more to it than is described here and it’s worth trying.

Performance is better, and working without heavyweight ASP.Net controls and complex page lifecycles is refreshing.

That said, Microsoft insists Web Forms aren’t going away, so it’s safe to stick with that approach if you prefer, or to benefit from its maturity and rich control set.

Resources
ASP.Net MVC

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Hands on: Putting Silverlight to work

Microsoft’s cross-platform plug-in now includes the .Net runtime

Hands on: Visual Studio 2008

Find out what’s new in Visual Studio 2008

Hands On - Web development - To Flash or not to Flash

A reader's query about problems with their browser's Back button when using framesets got me thinking about changing web standards. The Back button may seem a minor detail, but the problem...

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Virtual drive

A set of files seen by Windows as a separate hard disk.

Great shopping deals from Computeractive