How to change list index of TemplateInfo.HtmlFieldPrefix?

How to change the HtmlFieldPrefix index?

I get Children[0]from EditorFor (), and I want to make it Children[@Model.Id]
or Children[2].Children[4]from EditorFor (), and I want to do itChildren[@"ParentModel".Id].Children[@Model.Id]

I will not know the actual prefix until executed. Preferably, is there a built-in way to change it?
Or just messing around with a string? I'm still new to C # string functions.

+5
source share
1 answer

Try placing the following inside an editor template:

@model SomeViewModel
@{        
    ViewData.TemplateInfo.HtmlFieldPrefix = Regex.Replace(
        ViewData.TemplateInfo.HtmlFieldPrefix, 
        @"\[[0-9]+\]$", 
        m => string.Format("[{0}]", Model.Id)
    );
}
+8
source

All Articles