WebViewPage.ViewData之间的区别和@ Html.ViewData [英] Difference between WebViewPage.ViewData and @Html.ViewData

查看:360
本文介绍了WebViewPage.ViewData之间的区别和@ Html.ViewData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道什么之间的实际差异的ViewData 绑定到MVC的视图和的ViewData 即绑定到 @Html 辅助对象?

Just wondering what the actual difference between the ViewData that is bound to the MVC view and the ViewData that is bound to the @Html helper object?

我写了一个页面,他们似乎并不指同样的事情。用于其他任何地方申请的另一个字典下的同名隐藏的ViewData

I have written a page and they don't seem to refer to the same thing. Is ViewData used anywhere else in the application as another dictionary hidden under the same name?

推荐答案

答案很简单:结果
的HtmlHelper 的ViewData 基于视图的数据。因此,它具有在进入视图code相同的值(例如,剃刀或ASPX页面)。但你可以改变这些的ViewData 取值分别。

SHORT ANSWER:
The HtmlHelper's ViewData is based on the view's data. So it has same values upon entering view code (for example, Razor or ASPX page). But you can change these ViewDatas separately.

它用于同样的方式在 AjaxHelper

的RepeaterItem 有它自己的ViewData的,这是基于该项目。

RepeaterItem has it's own ViewData, which is based on the item.

我还没有发现任何利用不同的的ViewData 的任何地方。

I have not found any use of different ViewData anywhere.

更新:结果
的ViewData @ Html.ViewData 只有当你使用一个强类型的观点是不同的。如果使用不是强类型的观点,无论他们是平等作为参考。因此,我认为这样做是为了包裹的ViewData 到强类型的ViewDataDictionary<>

UPDATE:
ViewData and @Html.ViewData are different only when you use a strongly typed view. If you use a not strongly typed view, both they are equal as reference. So I think this was done to wrap the ViewData into strongly typed ViewDataDictionary<>.

一些调查

我在反编译源采取一看,这里是我发现了什么。

I have taken a look at the decompiled sources and here is what I found.

让我们来看看,什么是@ Html.ViewData:

Let's see, what is @Html.ViewData:

namespace System.Web.Mvc
{
  public class HtmlHelper<TModel> : HtmlHelper
  {
    private ViewDataDictionary<TModel> _viewData;

    public ViewDataDictionary<TModel> ViewData
    {
      get
      {
        return this._viewData;
      }
    }

    public HtmlHelper(ViewContext viewContext, IViewDataContainer viewDataContainer)
      : this(viewContext, viewDataContainer, RouteTable.Routes)
    {
    }

    public HtmlHelper(ViewContext viewContext, IViewDataContainer viewDataContainer, RouteCollection routeCollection)
      : base(viewContext, viewDataContainer, routeCollection)
    {
      this._viewData = new ViewDataDictionary<TModel>(viewDataContainer.ViewData);
    }
  }
}

正如我们看到的,的ViewData 是从一些 viewDataContainer 的实例的HtmlHelper 构造函数。

As we see, the ViewData is instantiated from some viewDataContainer in HtmlHelper constructor.

让我们尝试看看,这是怎么与页连接:

Let's try to see, how is this connected with the page:

namespace System.Web.Mvc { 

    public abstract class WebViewPage<TModel> : WebViewPage {

        // some code

        public override void InitHelpers() {
            base.InitHelpers(); 

            // ...

            Html = new HtmlHelper<TModel>(ViewContext, this);
        } 

       // some more code
    }
}

所以当前页是 viewDataContainer

所以,我们看到,一个的ViewData 词典的新实例的实例为的HtmlHelper 基于字典,它存储在查看。唯一的选择,这可能使这两个还挺一样的,如果他们使用相同的 Disctionary 内部。让我们来看看这一点。

So, we see, that a new instance of a ViewData dictionary is instantiated for HtmlHelper based on the dictionary, which is stored in View. The only option, which could make the two be kinda same, if they used same Disctionary internally. Let's check that.

下面是的ViewData 构造器:

    public ViewDataDictionary(ViewDataDictionary dictionary) 
    {
        if (dictionary == null) { 
            throw new ArgumentNullException("dictionary");
        } 

        foreach (var entry in dictionary) {
            _innerDictionary.Add(entry.Key, entry.Value); 
        }
        foreach (var entry in dictionary.ModelState) {
            ModelState.Add(entry.Key, entry.Value);
        } 

        Model = dictionary.Model; 
        TemplateInfo = dictionary.TemplateInfo; 

        // PERF: Don't unnecessarily instantiate the model metadata 
        _modelMetadata = dictionary._modelMetadata;
    }

我们可以看到,条目的抄袭,而是一种不同的底层 _innerDictionary 被使用。

这篇关于WebViewPage.ViewData之间的区别和@ Html.ViewData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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