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

1

5

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.

Coherence answered 10/8, 2011 at 23:46 Comment(0)
R
10

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)
    );
}
Rangefinder answered 11/8, 2011 at 9:30 Comment(2)
That's great! I figured it was a regular expression kind of thing. Thanks for showing me how.Coherence
lovely solution. I dislike mvc because it has not everything in it, But it has flexibility to do almost anythings you want.Terminology

© 2022 - 2024 — McMap. All rights reserved.