MVC Binding嵌套列表

时间:2013-01-20 01:55:19

标签: asp.net-mvc-3 model-binding

我有三个实体Package,Feature和PackageFeature,如下所示:

套餐:
PackageId(PK)
PackageTitle

特点:
FeatureId(PK)
FeatureTitle

PackageFeature:
PackageFeatureId(PK)
PackageId(FK)
特征(FK)
数量

我有一个键入“Package”的MVC视图,并允许用户编辑PackageFeature实体上的Quantity字段,从而得到下面的源代码,如何在控制器中选择它?我已经尝试了[Bind(Include = "PackageFeature")]List<Package> packageList<PackageFeature> packageFeature但是当我在调试器中查看前者时,我得到了正确数量的列表项(4)但是它们的PackageIds都是0,因此它们的PackageFeature列表为0 item,后者还会生成4个项目的列表,这些项目不正确,因为有8个PackageFeatures - 但是PackageIds是正确的,其他所有项目都设置为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-->

编辑:

用于生成上述代码:

软件包/ 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>
}

软件包/ 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 个答案:

答案 0 :(得分:0)

我在PackageFeature.cshtml模板中解决了这个问题,但是如果有人的话,我更喜欢更精简的开箱即用解决方案。

@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>
}