In the twist, I wrote this line @Html.EditorFor(x=>x.Pass, "My") where My is a specific template stored in EditorTemplate. Pass - has a type of string. The template code itself is simple:

 @model string @Html.Editor("BlaBla") - проверка запуска шаблона 

I expect to receive at the exit the input field and the inscription "check the launch of the template." But in return I get only an inscription. And instead of an input field - emptiness. If, of course, replace the helper with <input... > then the field will appear. The question is, why does the @Html.Editor("BlaBla") not work in the template @Html.Editor("BlaBla") all, it’s also the idea that creates an input with the name BlaBla?

upd

 @model test.Models.viewmodel @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> @using(Html.BeginForm()) { @Html.EditorFor(x=>x.Pass, "My")<br /> @Html.Editor("Test")<br /> <input type="submit" name="Send" /> } <br /> @Html.DisplayFor(x => x.name)<br /> @Html.DisplayFor(x => x.mail)<br /> @Html.DisplayFor(x => x.AnyNum)<br /> @Html.DisplayFor(x => x.Pass)<br /> <br /> </div> </body> </html> 

The viewmodel class, which is passed as a model in a view, does not contain a property named Test, but the input field is still created in a normal view using @Html.Editor("Test") . But in a template that accepts a model of the string type (which also does not contain the Test property) - already the input field @Html.Editor("Test") not created

    1 answer 1

    Not exactly, the @Html.Editor("BlaBla") method will try to create an input for the property defined in the model class, and the BlaBla argument sets the name of this property. The model is of type string , the System.String class does not contain a BlaBla property BlaBla , so the value is not displayed.

    In this case, to create an input with a given name and value, you need to use the TextBox function:

     @Html.TextBox("BlaBla", Model) 

    (Note that by default the name from TextBox will be merged through the dot with the name of the property from EditorFor . To avoid this, you must specify an empty string with the third argument: @Html.EditorFor(x=>x.Pass, "My", "") . )

    • Thanks for the answer, but why then does this helper work in a regular view? If in a normal view to which a model that does not contain a property with the name Test is written write @Html.Editor("Test") , then an input field is created in the name and ID 'Test', and this input field is not created in the template. I updated my question - Polyakov Sergey