2

我对此进行了一些研究,似乎发现了一些过时的帖子,或者在我的情况下不太适用。我可能在搜索时使用了错误的关键字... =/

在我的网页上,我有包含组框的选项卡,其中包含行,而行又包含项目。所以它是 4 级列表的列表。

问题:

回发时, ViewModel.Tabs 为空,我无法保存任何内容。一切都很好地显示,但没有任何东西被发回。

编码

视图/共享/EditorTemplates/Tab.cshtml

@model AWMCCRM.Web.ViewModels.Tab

@Html.HiddenFor(vm => vm.Name)
<div id="tab-@Model.Name.Replace(" ", string.Empty)" class="tab-content two">
     @Html.EditorFor(vm => vm.Groups)
</div>

视图/共享/EditorTemplates/Group.cshtml

@model AWMCCRM.Web.ViewModels.Group
@Html.HiddenFor(vm => vm.Name)
<fieldset>
    <legend>@Model.Name</legend>
    @Html.EditorFor(vm => vm.Lines)
</fieldset>

视图/共享/EditorTemplates/Line.cshtml

@model AWMCCRM.Web.ViewModels.Line
<div class="_100Max">
@Html.HiddenFor(vm => vm.Name)
@Html.EditorFor(vm => vm.Items)
</div>

视图/共享/EditorTemplates/Item.cshtml

@model AWMCCRM.Web.ViewModels.Item

<div class="_@Model.DisplaySize" title="@Model.Description">
    <p>
        @Html.HiddenFor(x => x.DataType)
        @Html.HiddenFor(x => x.Description)
        @Html.HiddenFor(x => x.DisplaySize)
        @Html.HiddenFor(x => x.DisplayType)
        @Html.HiddenFor(x => x.IDsPiped)
        @Html.HiddenFor(x => x.ItemType)
        @Html.HiddenFor(x => x.Name)

        @Html.LabelFor(vm => vm.Value, Model.Name)
        @switch (Model.DisplayType.ToLower().Replace(" ", string.Empty))
        {
            case "checkbox":
                @Html.CheckBoxFor(vm => Convert.ToBoolean(vm.Value))
                break;
            case "dropdownlist":
                @Html.DropDownListFor(vm => vm.Value, Model.ValueOptionListItems)
                break;
            case "multiselectlist":
                @Html.ListBoxFor(
                    x => x.SelectedValueList,
                    Model.ValueOptionListItems, 
                new { id = "itemValuesMultiSelect", multiple = "multiple", Size = 15 })
                break;
            case "radiobutton":
                @Html.RadioButtonFor(vm => vm.Value, Model.Value)
                break;
            case "textarea":
                @Html.TextAreaFor(vm => vm.Value)
                break;
            default:
                @Html.TextBoxFor(vm => vm.Value)
                break;
        }
    </p>
</div>

ViewModel(精简版)

namespace AWMCCRM.Web.ViewModels
{
    public class PersonEditViewModel
    {
        public List<Tab> Tabs { get; set; }

        //Other properties
        //...

    }
}

观点(精简版)

@using (Html.BeginForm("Edit", "Person", FormMethod.Post, new { id = "validate-form", @class = "block-content form" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(x => x.PersonID)
    @foreach (var tab in Model.Tabs)
    {  
        @Html.EditorFor(vm => tab)
    }
    <input class="close-toolbox button" type="submit" value="Save">
}

有什么建议么?

谢谢

4

1 回答 1

4

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

这是一篇旧文章,但它仍然适用。

我遇到了这个确切的问题,但基本上由于模型绑定系统,您需要使用显式 for 循环而不是 foreach 循环,并通过索引引用您的元素。

@using (Html.BeginForm("Edit", "Person", FormMethod.Post, new { id = "validate-form", @class = "block-content form" }))
{
    Html.AntiForgeryToken()
    Html.HiddenFor(x => x.PersonID)
    for (int i = 0; i<Model.Tabs.Count; i++)
    {
        Html.EditorFor(x => Model.Tabs[i])
    }
    <input class="close-toolbox button" type="submit" value="Save">
}
于 2013-08-02T08:50:01.230 回答