如果一个ASP.NET母版从视图中获得的数据? [英] Should an ASP.NET masterpage get its data from the view?

查看:150
本文介绍了如果一个ASP.NET母版从视图中获得的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩弄ASP.NET MVC包含母版页的站点。

I've been playing around with ASP.NET MVC with a site containing a Master Page.

我创建了一个叫做MVC的用户控件 ProductThumbnailControl 。用户控件显示产品和一个小缩略图。该视图是一个 ViewProduct 视图,它会显示完整的产品信息 - UserControl的仅仅是一个UI元素关闭该网站。

I created an MVC user control called ProductThumbnailControl. The user control displays a product and a small thumbnail image. The View is a ViewProduct view which displays full product information - the usercontrol just being a UI element off to the site.

 public partial class ProductThumbnailControl :
                      System.Web.Mvc.ViewProductControl<ViewProductsModel>
 {

 }

我读了博客条目,显示用户控件如何消费的ViewData 。我了解到,用户可以控制的自动地的获取从父视图的模型。由于它消耗相同的数据视图并不需要显式地传递给任何用户控件,使得清洁标记和code。

I read a blog entry that shows how user controls consume ViewData. I learned that the user control can automagically get its model from the parent View. Since it's consuming the same data the View doesn't need to explicitly pass anything to the user control, making for cleaner markup and code.

所以,现在我明白了,母版页使用相同的的ViewData 的页面。这意味着意味着母版页本身并没有真正有一个模型,以帮助使自己。

So now I've learned that the Master Page is using the same ViewData as the page. This means means the Master Page itself doesn't really have a model to help render itself.

请告诉我正确的方式为母版页来获得它的数据摆在首位?

Whats the correct way for a Master Page to get its data in the first place?

我想过尝试下?

您可以有一个 SiteModel

//Arbitrary properties for example
class SiteModel 
{
    public string PartnerId {get; set;}
    public ShoppingCart ShoppingCartContents {get; set;}    
    public string CurrentUserId {get; set;}
}

视图继承自它:

The View inherits from it:

class ViewProductModel : SiteModel 
{
    public Product Product {get; set;}
}

SiteModel 将由母版页被消耗掉。如果需要查看可以使用的数据,从它 - 如果他们需要的地方显示当前用户的电子邮件

SiteModel would be consumed by the Master Page. Views could use data from it if needed - if they needed to display the current user's email somewhere.

这是一个可怕的想法?

Is this a horrible idea?

如果母版页刚刚得到任何地方需要它的数据呢?

Should the master page just get its data from wherever it needs it?

如果我想包括在报头的用户控件

如果将它得到了的ViewData 从既然没有为whle页面只有一个的ViewData 对象?

Where would it get its ViewData from since there is only one ViewData object for the whle page?

请问我要使用这个,我恨可怕的语法,并通过母版页的用户控制一个明确的模式?

Would I have to use this horrible syntax that I hate and pass the master page's user control an explicit model?

Html.RenderUserControl("~/Views/Account/UserControls/Header.ascx",
  null, new { SelectedItem = "Profile" })

什么是解决这种情况的最好方法是什么?

What's the best way to tackle this scenario?

推荐答案

我们的母版页需要的数据视图。我们使用强类型的视图名,了解我们的观点和该方法的实施,我们还补充说,每一个页面的持久对象需要标准视图中的数据(如应用程序菜单结构信息,用户信息,显示在屏幕上,等等)。

Our master page takes data from the view. We use strongly typed view names for our views and in the methods for that implementation we also add standard view data that every page needs from our persistent objects (like the application menu structure info, the user info to display on screen and so forth).

这的确有可能使我们认为数据的母版页不强类型就像是模型对象的缺点,但它工作对我们非常好。

This does have the disadvantage of making our view data for the master page not strongly typed like it is for the Model object, but it works very well for us.

你的点子也不错。由于母版页的想法是类似的继承的想法,为什么不使用继承来设定你的模型对象。你可以把它更进了一步,创造产生的模型对象,并设置基类的数据模型厂,而它这样做。

Your idea is also good. As the idea of the Master page is similar to the idea of inheritance, why not use inheritance to setup your model objects. You could take it a step further and create a model factory that produces your model objects and sets the base class data while it does so.

这篇关于如果一个ASP.NET母版从视图中获得的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆