Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use strongly typed views?

I am working on an MVC app and I am wondering in what situation it is best to use a strongly typed view and when not... I guess this is more of a best practice question. I am making an ecommerce app and there is a table for orders, products, etc. The part I was working on that brought me to this question was my add a new product page for the admins to put more store items in. Any tips about when to know to use strongly typed view will be great help.

I already searched for related questions and nothing popped out in the first 3 pages or so, but if you know of a post please direct me.

Thanks.

like image 997
MetaGuru Avatar asked Sep 15 '09 14:09

MetaGuru


People also ask

What is the use of strongly typed views?

Strongly typed views are used for rendering specific types of model objects, instead of using the general ViewData structure. By specifying the type of data, you get access to IntelliSense for the model class.

What are the benefits of a strongly typed view in ASP.NET MVC?

Strongly typed views are used for rendering specific types of model objects. By specifying type of data, visual studio provides intellisense for that class. View inherits from ViewPage whereas strongly typed view inherits from ViewPage where T is type of the model.

What is the difference between strongly typed and weakly typed view in MVC?

In ASP.NET MVC, we can pass the data from the controller action method to a view in many different ways like ViewBag, ViewData, TempData and strongly typed model object. If we pass the data to a View using ViewBag, TempData, or ViewData, then that view becomes a loosely typed view.

What is strongly typed view model?

What is Strongly Typed View. The view which binds to a specific type of ViewModel is called as Strongly Typed View. By specifying the model, the Visual studio provides the intellisense and compile time checking of type. We learnt how to pass data from Controller to View in this tutorial.


2 Answers

Any time you need to show data (on any particular object or collection of objects) on the view, use a strongly typed view.

If your view is purely informational, you may be able to use the ModelState to pass small bits of information (eg: Success/Error pages, Not Authorized Messages, and etc.)

In my applications, I have EVERY view strongly typed, so that I can easily pass user login information to the Master Page. That is, all of my views are strongly typed, templated and constrained to a base class that contains the Site Configuration and the User Login info.

Because of that, I can do this:

public class MyBaseMasterPage : ViewMasterPage<MyBaseModel>
{
    public string CurrentTheme
    {
        get
        {
            if (this.Model.CurrentUser != null)
                return this.Model.CurrentUser.Theme;

            else return this.Model.Config.DefaultTheme;
        }
    }

    public User CurrentUser { get { return this.Model.CurrentUser; } }

    public ConfigurationRepository Config { get { return this.Model.Config; } }
}

Note that, since the Master Page is themed based on ONLY what is populated in the model, the View itself will never trigger a hit to the database/cache.

MyBaseModel is configured like so:

public class MyBaseModel
{
    private MyBaseModel() { }

    public MyBaseModel(MyBaseController controller)
    {
        this.CurrentUser = controller.CurrentUser;
        this.Config = controller.Config;
    }

    public User CurrentUser { get; private set; }

    public ConfigurationRepository Config { get; private set; }
}

The private constructor forces all subclasses of my model to initialize the model with the soruce controller.

The controller base class pulls the user out of session and the Config out of cache.

That way, no matter what, all of my views have access to the user and config data, without ever generating a hit to the DB.

Now, in MyBaseController:

public class LanLordzBaseController : Controller
{
    [Obsolete]
    protected new ViewResult View(string viewName, object model)
    {
        if (model == null)
        {
            throw new ArgumentNullException("model");
        }

        if (!(model is MyBaseModel))
        {
            throw new ArgumentException("The model you passed is not a valid model.", "model");
        }

        return base.View(viewName, model);
    }

    protected ViewResult View(string viewName, MyBaseModelmodel)
    {
        if (model == null)
        {
            throw new ArgumentNullException("model");
        }

        return base.View(viewName, (object)model);
    }

    public ConfigurationRepository Config { get { ... } }

    public User CurrentUser { get { ... } }
}

This helped me find all of my controllers that were returning views that weren't inherited from the proper base class.

like image 80
John Gietzen Avatar answered Nov 13 '22 17:11

John Gietzen


If there is data involved, there should be a strongly typed view. Period.

like image 44
Wyatt Barnett Avatar answered Nov 13 '22 18:11

Wyatt Barnett