I have a peculiar situation in which referencing 'model in my editor template produces a malformed index 'name' field:
Editor
@model IList<BillingRateItem>
@for (int i = 0;i < this.Model.Count(); i++)
{
@Html.HiddenFor(m => this.Model[i].BillingRateItemID)
}
The hidden field produced includes a period before the index, which is problematic:
HTML Rendered
<input id="Model_BillingRateItems__0__BillingRateItemID" name="Model.BillingRateItems.[0].BillingRateItemID" type="hidden" value="9">
What I want Rendered
Note that 'Model.BillingRateItems[0]' has no period between the name and the index. This is good!
<input id="Model_BillingRateItems__0__BillingRateItemID" name="Model.BillingRateItems[0].BillingRateItemID" type="hidden" value="9">
EDIT - This is an example of the view calling the editor
@model BillingRateViewModel // has multiple BillingRateItems
@Html.EditorFor(m=>m.BillingRateItems,"BillingRateItemsGrid")
Note: When I change the Editor to accept the parent object (e.g. BillingRate) the indexing works fine. Maybe the conclusion is simply the all editors will begin with a '.' regardless of the context. In the case of Enumerables it will add the '.[].' even though this does the native model binder no help in reconstructing the object.
I don't know exactly wich is the issue with the version of the MVC assembly that you are using, but looks like some bug that I can't reproduce on the latest version that you can install from the Visual Studio Extensions and Updates menu.
You can create this hidden field using the @Html.Hidden
this way:
@Html.Hidden("[" + i + "].BillingRateItemID", this.Model[i].BillingRateItemID);
The produced HTML will look like, where i
will be the index of each item and the value will assume the BillingRateItemID
:
<input name="[1].BillingRateItemID" type="hidden" value="1">
Then the default Model Binder will take care about the list correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With