Asp.net mvc 如何在editortemplate中访问父对象?

Asp.net mvc 如何在editortemplate中访问父对象?,asp.net-mvc,asp.net-mvc-2,editortemplates,Asp.net Mvc,Asp.net Mvc 2,Editortemplates,在自定义编辑器模板中,我希望访问父对象 我正在使用此代码,但这可能不是最好的方法,尤其是在使用嵌套视图时: object parent = ViewContext.Controller.ViewData.Model; 有人有更好的想法吗?如果编辑器需要额外的数据,请将其添加到模型中或使用ViewData,则不应尝试向模型层次结构升级。 对渲染编辑器的调用类似于 <%: Html.EditorFor(model => model.EditorModel, new {viewDataK

在自定义编辑器模板中,我希望访问父对象

我正在使用此代码,但这可能不是最好的方法,尤其是在使用嵌套视图时:

object parent = ViewContext.Controller.ViewData.Model;

有人有更好的想法吗?

如果编辑器需要额外的数据,请将其添加到模型中或使用ViewData,则不应尝试向模型层次结构升级。 对渲染编辑器的调用类似于

<%: Html.EditorFor(model => model.EditorModel, new {viewDataKeyName = Model.AdditionalData})%>
model.EditorModel,新的{viewDataKeyName=model.AdditionalData})%>
以这种方式添加对编辑器至关重要的数据时要小心,因为它必须包含在对该模板的每次调用中,这就是为什么我更喜欢将值包含在模型本身中