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
Related articles
Q.Why are some of the keys on my keyboard doing strange...
Q.Is my phone’s Bluetooth any use?
Q.Can I switch boot drives so that I can work on older...
St Helena, a 'small British village' in the mid-Atlantic, is seeking support and funding for a broadband connection
|
|
|
|
|
Computeractive Excel (2010) Online tutorialPrice: £19.99 |
Computeractive Word (2010) Online TutorialPrice: £19.99 |
Computeractive Powerpoint (2010) Online TutorialPrice: £19.99 |
Angry BirdsPrice: £9.99 |
Back Issue CD-Rom 14 (2011)Price: £15.99 |