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.
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" })
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.