Here's what you do, you create it as an editor template, because as Andre pointed out, HiddenFor
is equivalent to the helper methods like TextBoxFor
and CheckboxFor
.
It's likely that you'll want to have an actual editor too, so place your real editor under ~/Shared/EditorTemplates
. We're going to put our "hidden editor" under the controller you wish to use it on.
~/Views/ControllerName/EditorTemplates/ModelName.cshtml
Lets say we have a Person
model.
public class Person
{
public string First { get; set; }
public string Last { get; set; }
}
We'll create a partial view.
@Model Person
@Html.HiddenFor(p => p.First);
@Html.HiddenFor(p => p.Last);
And then we'll pretend we have a model that contains a Person
as a property. From our main view, we call our "hidden editor" like so.
@Model Foo
@Html.EditorFor(f => f.Person)
Easy peasy lemon squeezy. A bit hacky, but it works like a charm.