Aracılığıyla paylaş


Dynamic V Strongly Typed Views

There are three ways to pass information from a controller to a view in ASP.NET MVC 3:

  1. As a strongly typed model object.
  2. As a dynamic type (using @model dynamic)
  3. Using the ViewBag

I’ve written a simple MVC 3  Top Blog application to compare and contrast dynamic and strongly typed views. The controller starts out with a simple list of blogs:

 using System.Collections.Generic;
using System.Web.Mvc;

namespace Mvc3ViewDemo.Controllers {

    public class Blog {
        public string Name;
        public string URL;
    }

    public class HomeController : Controller {

        List<Blog> topBlogs = new List<Blog>
      { 
new Blog { Name = "ScottGu", URL = "https://weblogs.asp.net/scottgu/"},
new Blog { Name = "Jon Galloway", URL = "https://weblogs.asp.net/jgalloway"},
new Blog { Name = "Scott Hanselman", URL = "https://www.hanselman.com/blog/"}
      };

        public ActionResult IndexNotStonglyTyped() {
            return View(topBlogs);
        }

        public ActionResult StonglyTypedIndex() {
            return View(topBlogs);
        }

        public ActionResult IndexViewBag() {
            ViewBag.BestBlogs = topBlogs;
            return View();
        }
    }
}

Right-click in the IndexNotStonglyTyped() method and add a Razor view.

alt

Make sure the Create a strongly-typed view box is not checked. The resulting view doesn’t contain much:

 @{
    ViewBag.Title = "IndexNotStonglyTyped";
}

<h2>IndexNotStonglyTyped</h2>

On the first line of the Views\Home\IndexNotStonglyTyped.cshtml file, add the model directive and
the dynamic keyword.

 @model dynamic

Because we’re using a dynamic and not a strongly typed view, intellisense doesn’t help us. The completed code is shown below:

 @model dynamic
           
@{
    ViewBag.Title = "IndexNotStonglyTyped";
}

<h2>Index Not Stongly Typed</h2>

<p>
 <ul>
@foreach (var blog in Model) {
   <li>
    <a href="@blog.URL">@blog.Name</a>
   </li>   
}
 </ul>
</p>

alt

Now we’ll add a strongly typed view. Add the following code to the controller:

 public ActionResult StonglyTypedIndex() {
    return View(topBlogs);
}

Notice it’s exactly the same return View(topBlogs); call as the non-strongly typed view. Right click inside of StonglyTypedIndex() and select Add View. This time select the Blog Model class and select List as the Scaffold template.

alt

Inside the new view template we get intellisense support and our view model is automatically scaffolded. Those are significant advantages and why ASP.NET MVC applications typically use strongly typed views. Strongly-typed view gives you:

alt

Another non-strongly typed way we can pass the top blogs into a view template is to use the view bag.  ViewBag is new to MVC 3 and has the advantage that it can be used in combination with a strongly typed model, giving you the advantages for both. ViewBag is useful when you want to pass information not related to the view model and you don’t want to create a view model just to pass the information. For example, you can use it to pass information to your layout template. Be sure to read ScottGu’s post where he talks about ViewBag.

Add the following action method to the controller:

 public ActionResult IndexViewBag() {
            ViewBag.BestBlogs = topBlogs;
            return View();
        }

The IndexViewBag.cshtml view template :

 @{
    ViewBag.Title = "Index_ViewBag";
}

<h2>Index View Bag</h2>

<p>
 <ul>
@foreach (var blog in ViewBag.BestBlogs) {
   <li>
    <a href="@blog.URL">@blog.Name</a>
   </li>   
}
 </ul>
</p>

Good ViewBag links:

The c# project can be downloaded here.

Comments

  • Anonymous
    January 30, 2011
    IMHO viewBad don't give the same advantage of strongly typed view. It's a shortcut for the viewdDictionary.

  • Anonymous
    February 03, 2011
    Great post. I just started learning asp.net mvc and this info on how to pass data to views using the razor syntax has been very helpful.

  • Anonymous
    June 02, 2011
    Well I would say that it's an all in one complete post. Thanks.

  • Anonymous
    March 01, 2012
    +3 to Rick's right clicking and +1 to Bugeo's typos. priceless to bot

  • Anonymous
    March 22, 2012
    Very nice and useful explanation! Thanks cheers from Portugal!

  • Anonymous
    June 05, 2012
    hi, good post and very well explained . check also some samples on below link paradise-coding.blogspot.com/.../mvc-strongly-typed-view.html

  • Anonymous
    October 31, 2012
    the link to turning on Compile time type checking is bad - find the article here: devermind.wordpress.com/.../aspnet-mvc-tip-turn-on-compile-time-view-checking

  • Anonymous
    November 07, 2012
    Very simple and useful explanation! Thanks. Cheers

  • Anonymous
    March 27, 2013
    this guy sure knows his stuff right!

  • Anonymous
    May 09, 2013
    stephenwalther.com/.../asp-net-mvc-tip-50-ndash-create-view-models.aspx

  • Anonymous
    May 17, 2013
    Since its been a few years since you posted this article you probably have long forgotten about it lol, but I would like to point out that the "Compile time type checking"  appears to be a dead link.

  • Anonymous
    June 30, 2013
    This is Really good  thanks , Rick_Anderson

  • Anonymous
    January 16, 2014
    Thanks very much. It is nice descriptive and easy to understand how to send data from controller to view in different ways. We can implement as per our requirement.

  • Anonymous
    January 22, 2014
    Thank u very much Rickandy.... It's very useful and easy to understand for begineers

  • Anonymous
    April 02, 2014
    Really very good post.But i have one question, We can also share data through ViewData to View.I think so this option is missed.

  • Anonymous
    May 27, 2014
    Great Post. But, i have a doubt, which one is better to use?

  • Anonymous
    December 14, 2014
    A Great way to Learn. This man has both, knowledge and skill to deliver

  • Anonymous
    August 24, 2015
    So I'm reading this a few years later and I'm curious why nobody stumbled upon the 'IndexNotStonglyTyped' typo? It is consistent I have to give you that.. but the missing 'r' (stRongly) is really getting me of my game.. haha :)