We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Simple Listbox

I have selectlist in my controller like this:
var lstCountry = new SelectList(new[]
            {
                    new { ID = "AR", Name = "Argentina"},
                    new { ID = "AT", Name = "Austria"},
                    new { ID = "AU", Name = "Australia"},
                }, "ID""Name");
            ViewBag.lstC = lstCountry;
            return View();
How can I bind the listbox in the view to that lstC?

Thanks!

3 Replies

ES Ezhil S Syncfusion Team November 15, 2016 07:24 AM UTC

Hi Jurlina, 

Thank you for contacting Syncfusion support. 

In order to bind generic SelectList type data to Collection type control, you will need to convert it to list. Please refer the below code sample, 
[CSHTML] 
@Html.EJ().ListBox("listboxsample").Datasource((IEnumerable<SelectListItem>)ViewBag.lstC).ListBoxFields(lf=> lf.Text("Text").Value("Value")) 
 
[C#] 
public ActionResult Index() 
{ 
    var lstCountry = new SelectList(new[] 
    { 
            new { ID = "AR", Name = "Argentina"}, 
            new { ID = "AT", Name = "Austria"}, 
            new { ID = "AU", Name = "Australia"}, 
        }, "ID", "Name").ToList<SelectListItem>(); 
    ViewBag.lstC = lstCountry; 
    return View(); 
} 

You can find simple sample with your shared code from the link here: http://www.syncfusion.com/downloads/support/forum/127414/ze/Listbox_127414-15933599  

For further references check the below, 
 
Please let us know if you need further assistance. 
 
Regards, 
Ezhil S 



BJ Bernard Jurlina November 20, 2016 07:41 AM UTC

Yes,

thank You Ezhil. 
It works.

Bernard.


ES Ezhil S Syncfusion Team November 21, 2016 04:33 AM UTC

Hi Jurlina, 

Thanks for the update. 

We are glad the issue has been resolved in your end. 
 
Please let us know if you need further assistance. 
 
Regards, 
Ezhil S 


Loader.
Live Chat Icon For mobile
Up arrow icon