Populating multiselect list with data MVC 3 .NET

asp.net asp.net-mvc c# entity-framework sql-server

Question

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.

1
1
9/26/2012 10:46:31 AM

Accepted Answer

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; }
}

A ListBoxFor requires 2 properties on your view model:

  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

and then in the view:

@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.

11
9/26/2012 11:15:09 AM

Popular Answer

i can select multiple value in your listbox so i will suggest to do this in another type

you can use like this below....

you use this js file..

<script type="text/javascript" src="@Url.Content("~/jquery-1.6.4.min.js")"></script>

    <script type="text/javascript" src="@Url.Content("~/chosen.jquery.js")"></script> 

<link href="@Url.Content("~/CSS/chosen.css")" rel="stylesheet" type="text/css" />

and this use in your view

<script type="text/javascript">
        $(document).ready(function() {
            $('.chzn-select').chosen();

        });
 </script>

@Html.ListBoxFor(model => model.CountryId,Model.Countries, new { @class = "chzn-select",data-placeholder="Choose a Country...", Style = "width: 150px;" })

you just bind your listbox and select your multiple value like this. see more for jquery choosen ... http://davidwalsh.name/jquery-chosen

enter image description here

i think this will help you..



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow