Pass value from EditorTemplate only to layout template, in mvc 6

there is a way to transfer the value from the EditorTemplate to its layout (not to the _Layout website, but the layout of the template)

in MVC 5 I had something like this: EditorTemplates / MyStr.cshtml

@{
    Layout = "_FieldLayout.cshtml";
    ViewData.ModelMetadata.AdditionalValues.Add("__idpostfix", "-cde");
}
@Html.CustomHelper("") 

CustomHelper uses 2 input tags, one for display / editing and the other for storing values, both have identifiers, there is -cde postfix on the display and _FieldLayout.cshtmlhad this:

@{
    var postfix = ViewData.ModelMetadata.AdditionalValues.ContainsKey("__idpostfix") ? ViewData.ModelMetadata.AdditionalValues["__idpostfix"] : string.Empty;
}
@Html.Label("", new {@for = ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty) + postfix})
... 

now in mvc 6 AdditionalValues ​​is a ReadOnly dictionary, so there is another way to do this, my best choice is to just use another FieldLayout when I need a postfix

+4
source share
1 answer

ViewData ( ).

EditorTemplates/MyStr.cshtml ViewData["somedata"] = "some value";, _FieldLayout.cshtml .

"" , , , (, ViewData), .

, ViewData " ", reset .

+1

All Articles