MVC Binding nested list

2019-08-30 11:31发布

问题:

I have three entities Package, Feature and PackageFeature as follows:

Package:
PackageId (PK)
PackageTitle

Feature:
FeatureId (PK)
FeatureTitle

PackageFeature:
PackageFeatureId (PK)
PackageId (FK)
Feature (FK)
Quantity

I have an MVC view that is typed to "Package" and allows the user to edit the Quantity field on the PackageFeature entity resulting in the source code below, how do I pick this up in the controller? I've tried [Bind(Include = "PackageFeature")]List<Package> package and List<PackageFeature> packageFeature but when I view the former in the debugger I get the correct number of list items (4) but their PackageIds are all 0 and consequently they have a PackageFeature list of 0 items, the latter also produces a list of 4 items which is incorrect as there are 8 PackageFeatures - however the PackageIds are correct, everything else is set to 0/null.

<form action="/packages/features/" method="post">

<h3>Basic</h3>

<input data-val="true" data-val-number="..." data-val-required="..." name="[0].PackageId" type="hidden" value="2" />

<ul>
    <li>
        <span>No. of Categories (e.g. Plumber)</span>
        <input data-val="true" data-val-number="..." name="[0].PackageFeature[8].Quantity" type="text" value="1" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[0].PackageFeature[8].PackageFeatureId" type="hidden" value="9" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[0].PackageFeature[8].PackageId" type="hidden" value="2" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[0].PackageFeature[8].FeatureId" type="hidden" value="9" />
    </li>
    <li>
        <span>No. of Regions (e.g. Sydney City)</span>
        <input data-val="true" data-val-number="..." name="[0].PackageFeature[9].Quantity" type="text" value="1" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[0].PackageFeature[9].PackageFeatureId" type="hidden" value="10" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[0].PackageFeature[9].PackageId" type="hidden" value="2" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[0].PackageFeature[9].FeatureId" type="hidden" value="10" />
    </li>
</ul>

<h3>Bronze</h3>

<input data-val="true" data-val-number="..." data-val-required="..." name="[1].PackageId" type="hidden" value="3" />

<ul>
    <li>
        <span>No. of Categories (e.g. Plumber)</span>
        <input data-val="true" data-val-number="..." name="[1].PackageFeature[8].Quantity" type="text" value="2" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[1].PackageFeature[8].PackageFeatureId" type="hidden" value="19" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[1].PackageFeature[8].PackageId" type="hidden" value="3" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[1].PackageFeature[8].FeatureId" type="hidden" value="9" />
    </li>


    <li>
        <span>No. of Regions (e.g. Sydney City)</span>
        <input data-val="true" data-val-number="..." name="[1].PackageFeature[9].Quantity" type="text" value="4" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[1].PackageFeature[9].PackageFeatureId" type="hidden" value="20" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[1].PackageFeature[9].PackageId" type="hidden" value="3" />
        <input data-val="true" data-val-number="..." data-val-required="..." name="[1].PackageFeature[9].FeatureId" type="hidden" value="10" />
    </li>
</ul>

<!-- above repeated again for 2 more packages-->

EDIT:

Code used to generate above:

Packages/Features.cshtml:

@model List<RenovateDTL.Package>

@{
    ViewBag.Title = "Package Features";
    Layout = "~/Views/Shared/_OneColumnLayout.cshtml";
}

<h2>Package Features</h2>

@using (Html.BeginForm())
{
    @Html.DisplayForModel()

    <button type="submit">Save Changes</button>
}

Packages/DisplayTemplates/Package.cshtml:

@model RenovateDTL.Package

<h3>@Model.Title</h3>

@Html.HiddenFor(m => m.PackageId)

<ul>
    @Html.EditorFor(m => m.PackageFeature)
</ul>

/Packages/EditorTemplates/PackageFeature.cshtml:

@model RenovateDTL.PackageFeature

@using RenovateDTL.Enum

@if (Model.FeatureId == (int)FeatureId.NumberOfCategoriesAllowed || Model.FeatureId == (int)FeatureId.NumberOfRegions)
{
    <li>
        <span>@Model.Feature.Title</span>
        @Html.TextBoxFor(m => m.Quantity)
        @Html.HiddenFor(m => m.PackageFeatureId)
        @Html.HiddenFor(m => m.PackageId)
        @Html.HiddenFor(m => m.FeatureId)
    </li>
}

回答1:

I did this as a work around in the PackageFeature.cshtml template but I'm I'd prefer a more streamlined out-of-the-box solution if anyone has one.

@model RenovateDTL.PackageFeature

@{
    ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "PackageFeature[" + Model.PackageFeatureId + "]";
}

@if (Model.FeatureId == (int)FeatureId.NumberOfCategoriesAllowed || Model.FeatureId == (int)FeatureId.NumberOfRegions)
{
    <li>
        <span>@Model.Feature.Title</span>
        <input type="hidden" name="PackageFeature.Index" value="@Model.PackageFeatureId" />
        @Html.TextBoxFor(m => m.Quantity)
        @Html.HiddenFor(m => m.PackageFeatureId)
        @Html.HiddenFor(m => m.PackageId)
        @Html.HiddenFor(m => m.FeatureId)
    </li>
}