Need help to retrive ViewBag.datasource

Hi,

I've downloaded sample from - MVC Music Store, https://archive.codeplex.com/?p=mvcmusicstore

This is the class,
**********************************

public class Album
{
    public virtual int AlbumId { get; set; }
    public virtual int GenreId { get; set; }
    public virtual int ArtistId { get; set; }
    public virtual string Title { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtUrl { get; set; }
    public virtual Genre Genre { get; set; }
    public virtual Artist Artist { get; set; }
}

public class Genre
    {
        public virtual int GenreId { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual List<Album> Albums { get; set; }
    }

 public class Artist
    {
        public virtual int ArtistId { get; set; }
        public virtual string Name { get; set; }
    }
**********************************



(Origin Page) - https://ibb.co/VYZw5gZ

(Replace Control with EJ ComboBox) - https://ibb.co/Br3xVhQ

I want to replace  @Html.DropDownList("GenreId", String.Empty) into

 @{
                    Html.EJ()
                        .ComboBox("selectGenre")
                        .Width("100%")
                        .Datasource((IEnumerable<MvcMusicStore.Models.Album>)ViewBag.datasource)
                        .ComboBoxFields(f => f.Text("Name").Value("GenreId"))
                        .Placeholder("Select a genre")
                        .Render();
                }



My problem is, I do not know the right syntax of Datasource Binding

" IEnumerable<MvcMusicStore.Models.Album>)ViewBag.datasource " is workable

What is the right syntax ?

Attachment: 24042021_01_31ba7511.rar

1 Reply 1 reply marked as answer

KR Keerthana Rajendran Syncfusion Team April 26, 2021 11:48 AM UTC

Hi KHAIRUL, 
 
Thanks for contacting Syncfusion support. 
 
We checked your query and would like to let you know that you can bind any IEnumerable list as dataSource for ComboBox. Refer to the following code similar to your scenario.   
 
<div class="col-md-10"> 
                @{ 
                    Html.EJ() 
                        .ComboBox("selectGenre") 
                        .Width("300px") 
                        .Datasource((IEnumerable<Album>)ViewBag.datasource) 
                        .ComboBoxFields(f => f.Text("Title").Value("GenreId")) 
                        .Placeholder("Select a genre") 
                        .Render(); 
                } 
            </div> 
 
public ActionResult Edit() 
        {             
            ViewBag.datasource = Album.GetAlbum(); 
 
            return View(); 
        } 
 
   public class Album 
        { 
            public virtual int AlbumId { get; set; } 
            public virtual int GenreId { get; set; } 
            public virtual int ArtistId { get; set; } 
            public virtual string Title { get; set; } 
            public virtual decimal Price { get; set; } 
            public virtual string AlbumArtUrl { get; set; } 
            public virtual Genre Genre { get; set; } 
            public virtual Artist Artist { get; set; } 
            public static List<Album> GetAlbum() 
            { 
                List<Album> album = new List<Album>(); 
                album.Add(new Album { GenreId = 1, Title = "Album 1" }); 
                album.Add(new Album { GenreId = 2, Title = "Album 2" }); 
                album.Add(new Album { GenreId = 3, Title = "Album 3" }); 
                album.Add(new Album { GenreId = 4, Title = "Album 4" }); 
                return album; 
            } 
        } 
 
We have attached a sample for your reference in the following link. 
 
 
Refer to the following demo link for local data binding with ComboBox.  
 
 
Refer to the following UG link for available dataSource binding and its sample code. 
 
 
Please let us know if you need further assistance.  
 
Regards, 
Keerthana.  


Marked as answer
Loader.
Up arrow icon