如何处理嵌套集合在MVC 5 [英] How to handle nested collections in MVC 5

查看:206
本文介绍了如何处理嵌套集合在MVC 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC 5,我希望有一个编辑页面,让所选择的项目从列表,与所选每个项目的选项。所以,我有这样一个模型:

I'm using MVC 5 and I want to have an edit page that allows selection of items from a list, with options for each item selected. So I have a model like this:

public class TemplateEdit
{
    public Guid TemplateId {get; set;}
    public string TemplateName {get; set;}
    public ICollection<MyOption> Options {get; set;}
}

public class MyOption
{
    public Guid MyOptionId {get; set;}
    public Guid TemplateId {get; set;}
    public string OptionName {get; set;}
    public bool Selected {get; set;}
    public ICollection<SubOption> SubOptions {get; set;}
}

public class SubOption
{
    public Guid SubOptionId {get; set;}
    public Guid MyOptionId {get; set;}
    public string SubOptionName {get; set;}
    public bool Selected {get; set;}
}

让我们假设我有LINQ获取数据这一点。现在我想以显示它。我希望用编辑模板。

Let's assume I have LINQ to get the data for this. Now I want to display it. I was hoping to use Editor templates.

在我的模板edit.cshtml 视图我有这样的事情:

In my template-edit.cshtml view I have something like this:

@model TemplateEdit
<div class="form-group col-md-12">
    @Html.ValidationSummary(false, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.TemplateId)
</div>

<div class="form-group col-md-12">
    @Html.LabelForRequired(m => m.TemplateName, new {@class = "control-label"})
    @Html.TextBoxFor(m => m.TemplateName, new {@class = "form-control"})
</div>

<div class="col-md-12" style="overflow: auto; height: 350px;">
    @Html.Label("Options Selected")
    <div>
        @Html.EditorFor(m => m.Options);
    </div>
</div>

MyOption.cshtml EditorTemplate:

In MyOption.cshtml EditorTemplate:

@model MyOption
<div class="optionable">

    @Html.HiddenFor(m => m.MyOptionId)
    @Html.HiddenFor(m => m.TemplateId)

    <div class="row">
        <div class="col-md-2">
            @Html.CheckBoxFor(model => model.Selected, new { @class = "form-control optionToggle" })
        </div>

        <div class="col-md-8">
            @Html.DisplayFor(model => model.OptionName, new { @class = "form-control" } )
        </div>
    </div>
    <div class="row options">
        @Html.EditorFor(m => m.SubOptions);
    </div>
</div>

在我的 SubOption.cshtml EditorTemplate:

In my SubOption.cshtml EditorTemplate:

@model SubOption
<div class="optionable">

    @Html.HiddenFor(m => m.SubOptionId)
    @Html.HiddenFor(m => m.MyOptionId)

    <div class="row">
        <div class="col-md-2">
            @Html.CheckBoxFor(model => model.Selected, new { @class = "form-control optionToggle" })
        </div>

        <div class="col-md-8">
            @Html.DisplayFor(model => model.SubOptionName, new { @class = "form-control" } )
        </div>
    </div>
</div>

我遇到的问题是,当我回来的型号,我没有得到的子选项的数据。

The problem I am having is that when I return the model, I am not getting the SubOption data.

注:的结果
所有我找到的例子是MVC3或更早版本,并使用索引方式,通过集合进行迭代。我会尝试,除了我使用的ICollection的集合。

Note:
All the examples I find are for MVC3 or earlier, and use an index method to iterate through the collection. I would try that, except I am using ICollection for the collections.

推荐答案

检查您的动作看起来像在你的控制器。如果你传递的视图模型的参数中,应该包括你的子选项的集合。

Check what your action looks like in your controller. If you're passing the viewmodel as the parameter it should include your 'SubOptions' collection.

    [HttpPost]
    public ActionResult SaveData(MyViewModel model)
    {
    }

这篇关于如何处理嵌套集合在MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆