绑定多选选项列出的对象 [英] Bind multiselect selections to list of objects

查看:122
本文介绍了绑定多选选项列出的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图模型像这样:

 公共类ListingPlanEditorViewModel
{
    公共ListingPlan计划{搞定;组; }
    公共IEnumerable的<名录及GT; SiteDirectories {搞定;组; }
}

一个属性的类型ListingPlan的对象在这里:

 公共类ListingPlan
{
    公众诠释? ListingPlanID {搞定;组; }
    公众诠释DescriptionLinesCount {搞定;组; }
    公开名单<名录及GT;目录{搞定;组; }
}

对象目录如下:

 公共类目录
{
    公众诠释DirectoryID {搞定;组; }
    公共字符串名称{;组; }
    公共字符串描述{搞定;组; }
}

我有一个控制器返回ListingPlanEditorViewModel的观点:

 公众的ActionResult ConfigurePlan(INT?listingIdentifier)
    {
        ListingPlan计划=新ListingPlan()
        {
            DescriptionLinesCount = 10,
            目录=新的List<名录及GT;()
            {
                新目录()
                {
                    DirectoryID = 3
                },
                新目录()
                {
                    DirectoryID = 4
                }
            }
        };
        ListingPlanEditorViewModel模式=新ListingPlanEditorViewModel()
        {
            计划=规划,// _ listingRepository.GetListingPlan(listingIdentifier,空)
            SiteDirectories = _database.GetDirectories()
        };        返回查看(模型);
    }

我想创建将选定值绑定回在ListingPlanEditorViewModel计划属性多选箱,设定各选择DirectoryID财产。所以结合后,我应该有Directory对象的列表。所有与他们DirectoryID的设定。

我有一些麻烦这样做。我可以创建一个有正确的选择选项的multiselectbox,但我无法找回它们在我的岗位行动看起来像这样:

  @using(Html.BeginForm(ConfigurePlan,ListingPlan))
{
    < D​​IV CLASS =表格体>
        @ Html.ListBoxFor(型号=> model.Plan.Directories,新MultiSelectList(Model.SiteDirectories,DirectoryID,姓名))
< / DIV><按钮式=提交>提交< /按钮>
}


解决方案

您必须创建一个 [] 列表 标识在将存储选定值视图模型的。

 公共类ListingPlanEditorViewModel
{
    公共ListingPlan计划{搞定;组; }
    公共IEnumerable的<名录及GT; SiteDirectories {搞定;组; }
    公众诠释[] {DirectoryIDs获取;设置;}
}

视图将根据改变。选定的目录将被存储在 DirectoryIDs

  @using(Html.BeginForm(ConfigurePlan,ListingPlan))
{
    < D​​IV CLASS =表格体>
        @ Html.ListBoxFor(型号=> model.DirectoryIDs,新MultiSelectList(Model.SiteDirectories,DirectoryID,姓名))
    < / DIV>    <按钮式=提交>提交< /按钮>
}

现在的 POST 动作就可以查询数据库,并获得目录,是由用户来选择。

注意::你不能只是得到的全对象的,因为 ListBoxFor 将生成一个<选择多...> ...< /选择方式> 标签将不知道如何绑定到你的对象

I have a view model like so:

public class ListingPlanEditorViewModel
{
    public ListingPlan Plan { get; set; }
    public IEnumerable<Directory> SiteDirectories { get; set; }
}

One property is an object of type ListingPlan here:

public class ListingPlan
{
    public int? ListingPlanID { get; set; }
    public int DescriptionLinesCount { get; set; }
    public List<Directory> Directories { get; set; }
}

The object Directory looks like this:

public class Directory
{
    public int DirectoryID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

I have a controller that returns a ListingPlanEditorViewModel to the view:

   public ActionResult ConfigurePlan(int? listingIdentifier)
    {
        ListingPlan plan = new ListingPlan()
        {
            DescriptionLinesCount = 10,
            Directories = new List<Directory>()
            {
                new Directory()
                {
                    DirectoryID = 3
                },
                new Directory()
                {
                    DirectoryID = 4
                }
            }
        };
        ListingPlanEditorViewModel model = new ListingPlanEditorViewModel()
        {
            Plan = plan,//_listingRepository.GetListingPlan(listingIdentifier, null),
            SiteDirectories = _database.GetDirectories()
        };

        return View(model);
    }

I would like to create a multiselect box that will bind the selected values back to the Plan property in the ListingPlanEditorViewModel, setting the DirectoryID property for each selection. So after binding I should have a List of Directory objects. All with their DirectoryID's set.

I'm having some trouble doing this. I can create the multiselectbox with the correct select options in it, but I am unable to retrieve them in my post action which looks like this:

@using (Html.BeginForm("ConfigurePlan", "ListingPlan"))
{
    <div class="form-body">
        @Html.ListBoxFor(model => model.Plan.Directories, new MultiSelectList(Model.SiteDirectories, "DirectoryID", "Name"))
</div>

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

解决方案

You have to create an [] or List of IDs in the ViewModel that will store selected values.

public class ListingPlanEditorViewModel
{
    public ListingPlan Plan { get; set; }
    public IEnumerable<Directory> SiteDirectories { get; set; }
    public int[] DirectoryIDs {get;set;}
}

The View will change according. The Directories selected will be stored in DirectoryIDs.

@using (Html.BeginForm("ConfigurePlan", "ListingPlan"))
{
    <div class="form-body">
        @Html.ListBoxFor(model => model.DirectoryIDs, new MultiSelectList(Model.SiteDirectories, "DirectoryID", "Name"))
    </div>

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

Now on POST Action you can query the database and get the Directories that was selected by user.

Note: You can't just get the full objects because the ListBoxFor will generate a <select multiple ... > ... </select> tag won't know how to bind to your object.

这篇关于绑定多选选项列出的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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