When to use strongly typed views?

I am working on an MVC application, and I wonder in which situation it is best to use a strongly typed representation, and when not ... I think this is a more important issue. I am making an e-commerce application, and there is a table for orders, products, etc. The part that I worked on led me to this question, I added a new product page for administrators to add more items to the store. when to know what to use a strongly typed view would be a big help.

I was already looking for related questions and didn't jump out anything on the first 3 pages or so, but if you know about the mail, please direct me.

Thanks.

+5
source share
6 answers

I use strongly typed views, when I can, I can get away from all the different bits of the ViewData in the view.

I create my own strongly typed views anytime I need information from more than one source for a view.

For example, in my Checkout, I need an order, but also user privileges to display prices; so I created a CheckoutViewModel that has an Order and PriceDisplay property.

Hope this helps,

Dan

+2
source

Each time you need to display data (on any particular object or collection of objects) in a view, use a strongly typed view.

, ModelState (: /, ..)

EVERY view, , . , , .

- :

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; } }
}

, , , /.

MyBaseModel :

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; }
}

soruce.

.

, , , .

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 { ... } }
}

, , .

+5

, . .

+3

. HtmlHelper. MvcContrib.

+1

, .

, .

- , , .

, MVC .

0

, , , . Model, View, Controller , , . , , , , .

ModelState TempState , , , .. , State , , , .

In my code-dependent types, they usually refer hierarchically to the base type, to which the type is strictly indicated, and therefore are available in the view where necessary.

0
source

All Articles