MVC3 ASP.NET - Strongly Typed View Fundamentals –“ you get out what you put in”Add to My Brief

Josh Doolan | 14/03/2011

Strongly typed views are a really handy feature of the ASP.NET MVC framework. Aside from the obvious advantage of binding form data to a particular class or view model, the ability to extend this concept to templates/views with complex data types is real winner (WINNING!  as Mr Sheen would say).
The way it fundamentally works is based on what you put in is what you get out. What I mean by this is if for instance you have a complex type such as this example of a person:

Public Class Person

{
 String _name;
 String _age;
 Pet _pet;

 // gets & sets below..etc
 }

If in the event you have a use case where you wanted to bind this particular class to a form (to create a “New” person), it would be pretty straight forward up until you got to the complex type Pet

What you would do is simply strongly type the person to a particular view and then within that view call a template or view file that is strongly typed to “Pet”. To assign a complex type to a template/view you simply utilise the html mvc helpers  Html.ControlFor. In this particular instance because you are not dealing with a simple html control like a text box you would use the:

Html.EditorFor(model => model.Pet, "Pet")  

Here you can see, you are putting an instance of the view model “pet” into the strongly typed view called “Pet” in order to get back a completely data filled view model when the original view form is submitted.

This is a very powerful way to simplify and encapsulate data in more complex and larger scale web sites.

 

tags: Web Development

Interested in learning more?

Wiliam is a leading supplier of web solutions and can provide expert advice to assist your business or organisation online.

Make enquiry   Request quotation

Comments

Share this page

Print this page Email this page to a friend Bookmark and Share

No Brief added yet.

My Brief results-driven approach emphasises a commitment to our clients needs through strategic analysis and best-practice.

Add to My Brief

Click on this icon to save item into My Brief. Retrieve, email and print

Drag & Drop

Drag & drop any iten with this icon into My Brief. Retrieve, email and print

Search Blogs