开发者

How to get to the parent object in an editortemplate?

In a custom editor template I want to access开发者_StackOverflow社区 the parent object.

I'm using this code but this is probably not the best way to do it, especially when using nested views:

object parent = ViewContext.Controller.ViewData.Model;

Does anybody have a better idea?


You shouldn't try climbing up the model hierarchy, if an editor requires extra data add that to the model or use ViewData. The call to render editor would look something like

<%: Html.EditorFor(model => model.EditorModel, new {viewDataKeyName = Model.AdditionalData})%>

Be careful when adding data that is vital to the editor this way, as it has to be included in each call to this template, that's why I prefer to include the values in the model itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜