用数据填充MVC 3 .NET多选名单 [英] Populating multiselect list with data MVC 3 .NET

查看:121
本文介绍了用数据填充MVC 3 .NET多选名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直对如何填充一个多选框,可以选择值搜索在谷歌和#2很多关于soltuions。但没有运气。

I have been searching a lot on Google and Stackoverflow for soltuions on how to Populate a Multiselect box with values that can be selected. But no luck.

public class PriceObj
{
    public int ID { get; set; }
    public decimal Price { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual ICollection<PriceGroupObj> PriceGroup {get; set;}
}

public class PriceGroupObj
{
   public int ID { get; set; }
   public string Name { get; set; }
}


public class PriceDatabaseContext : DbContext
{
    public DbSet<PriceObj> PriceObjs { get; set; }
    public DbSet<PriceGroupObj> PriceGroupObjs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();

        modelBuilder.Entity<PriceObj>()
            .HasMany(g => g.PriceGroup)
            .WithMany()
            .Map(t => t.MapLeftKey("GroupID").MapRightKey("PriceID").ToTable("PriceGroup"));
    }


}

然后我想中可以选择所有拨款价格群体在价格对象的编辑视图的代价。但我不明白它的工作,它不填充所有已创建集团在多重选择框。

Then I want in to be able to choose all appropriated price groups for a price in the edit view of the price objects. But I do not get it working, it is not populating all the already created groups in the MultiSelect box.

这是code,我使用的剃刀教职员:

This is the code I am using in the Razor veiw:

@Html.ListBoxFor(model => model.PriceGroup, Model.PriceGroup.Select(pg => new SelectListItem { Text = pg.Name, Value = pg.ID.ToString() }), new { Multiple = "multiple" })

select元素中显示的网站上,但没有值可供选择。

The select element shows up on the website, but no values are to select from.

请帮我整理一下,我的链下跌。

Please help me sort out, where my chain is falling of.

推荐答案

使用适于您观的要求视图模型:

Use a view model that is adapted to the requirements of your view:

public class PriceViewModel
{
    public int[] SelectedPriceIds { get; set; }
    public IEnumerable<SelectListItem> Prices { get; set; }
}

一个ListBoxFor需要在您的视图模型2个属性:

A ListBoxFor requires 2 properties on your view model:


  1. 为基本类型的集合属性(的String [] ID [] 的Guid [] ,...),将举行所选元素的ID

  2. 的IEnumerable&LT; SelectListItem&GT; 属性,将持有的所有项目的列表

  1. A collection property of a primitive type (string[], id[], Guid[], ...) that will hold the ids of the selected elements
  2. An IEnumerable<SelectListItem> property that will hold the list of all items

,然后在视图:

@Html.ListBoxFor(x => x.SelectedPriceIds, Model.Prices)

现在您的控制器的行动将这个视图模型传递给视图,而不是它只是没有适应你想实现你的域对象。这是,你会从你的域模型填充这个视图模型控制器的动作里面。

Now your controller actions will pass this view model to the view instead of your domain objects which simply are not adapted to what you are trying to achieve. It is inside the controller actions that you will populate this view model from your domain models.

这篇关于用数据填充MVC 3 .NET多选名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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