Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the list index of TemplateInfo.HtmlFieldPrefix?

How can I change the index of HtmlFieldPrefix?

I'm getting Children[0] from EditorFor() and I want to make it Children[@Model.Id]
or Children[2].Children[4] from EditorFor() and I want to make it Children[@"ParentModel".Id].Children[@Model.Id]

I won't know the actual prefix until runtime. Preferably there'd be a built-in way to change it?
Or just messing with the string? I'm still new to C# string functions.

like image 521
Benjamin Avatar asked Aug 10 '11 23:08

Benjamin


1 Answers

Try putting the following inside your editor template:

@model SomeViewModel
@{        
    ViewData.TemplateInfo.HtmlFieldPrefix = Regex.Replace(
        ViewData.TemplateInfo.HtmlFieldPrefix, 
        @"\[[0-9]+\]$", 
        m => string.Format("[{0}]", Model.Id)
    );
}
like image 145
Darin Dimitrov Avatar answered Oct 18 '22 12:10

Darin Dimitrov