Ben*_*min 5 c# model-binding mvc-editor-templates razor asp.net-mvc-3
如何更改HtmlFieldPrefix的索引?
我是Children[0]从EditorFor()获得的,我想制作它Children[@Model.Id]
或者Children[2].Children[4]来自EditorFor()而我想制作它Children[@"ParentModel".Id].Children[@Model.Id]
直到运行时我才会知道实际的前缀.最好有内置的方法来改变它吗?
或者只是弄乱字符串?我还是C#字符串函数的新手.
尝试将以下内容放在编辑器模板中:
@model SomeViewModel
@{
ViewData.TemplateInfo.HtmlFieldPrefix = Regex.Replace(
ViewData.TemplateInfo.HtmlFieldPrefix,
@"\[[0-9]+\]$",
m => string.Format("[{0}]", Model.Id)
);
}
Run Code Online (Sandbox Code Playgroud)