In ASP.NET MVC (5.x, not Core), I have a CSHTML page where the model contains a nested object for which I have input controls, similar to:
@Html.TextBoxFor(model => model.Order.CustomerName)
Done that way, ASP.NET prefixes the id and name attributes of the input tag with "Order", per:
<input id="Order_CustomerName" name="Order.CustomerName" type="text" value="...">
The prefixes mess up the form post. I can force the name and id to be just CustomerName by doing:
@Html.TextBoxFor(model => model.Order.CustomerName, new { id = "CustomerName", name = "CustomerName" })
but that's a nuisance to have to do that on every input control.
I could move all the input fields into a partial and invoke it with Model.Order as its model, but splitting out the content has some gotchas in my scenario.
Is there a way to specify to use Model.Order as the model for all controls within a form or a section of a page? Something like the following fictional directive:
@useModel(Model.Order) {
@Html.TextBoxFor(model => model.CustomerName)
}