ViewUserControl.ViewDataKey 到底是什么?
文档没有说太多。 http:// msdn.microsoft.com/en-us/library/system.web.mvc.viewusercontrol.viewdatakey(v=vs.98).aspx
ViewUserControl 的 ViewDataKey 属性的用途是什么以及如何利用它?
The documentation doesn't say much. http://msdn.microsoft.com/en-us/library/system.web.mvc.viewusercontrol.viewdatakey(v=vs.98).aspx
What is the purpose of the ViewDataKey property of ViewUserControl and how can it be leveraged?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ViewDataKey 属性可用于指定 ViewData 集合中要用于 ViewUserControl 模型的项目的键。基本上,控件不会默认继承父级的整个 ViewData 集合,而是仅获取具有您指定的键的项目。
因此,例如,如果您输入
ViewDataKey="EmployeeData"
,那么它将在 ViewData 中查找键为“EmployeeData”的项目,并将其用作 ViewUserControl 的 Model 对象。据我所知,这主要在使用声明性语法将 ViewUserControls 放入视图时使用。这意味着,您不使用
RenderPartial
,而是使用旧的 WebForm 语法在页面上注册用户控件,并使用类似
。The ViewDataKey property can be used to specify the key of the item in the ViewData collection that you want to use for the ViewUserControl's Model. Basically, instead of just inheriting the parent's entire ViewData collection by default, the control will just get the item with the key you specify.
So, for example, if you put
ViewDataKey="EmployeeData"
, then it would look for an item in ViewData with the key "EmployeeData", and it would use that as the Model object for your ViewUserControl.From what I can see, this is mainly used when using a declarative syntax for putting ViewUserControls inside a view. Meaning, instead of using
RenderPartial
, you use the old WebForm's syntax for registering a user control on a page, and have a server tag like<MyApp:MyUserControl runat="server" ViewDataKey="EmployeeData" />
.