Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在自定义编辑器模板中,我想访问父对象。
我正在使用这段代码,但这可能不是最好的方法,尤其是在使用嵌套视图时:
object parent = ViewContext.Controller.ViewData.Model;
有人有更好的主意吗?
如果编辑器需要额外的数据,请将其添加到模型或使用 ViewData,您不应该尝试爬上模型层次结构。对渲染编辑器的调用看起来像
<%: Html.EditorFor(model => model.EditorModel, new {viewDataKeyName = Model.AdditionalData})%>
以这种方式添加对编辑器至关重要的数据时要小心,因为它必须包含在对该模板的每次调用中,这就是为什么我更喜欢在模型本身中包含这些值。