Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I have a drop down box in my view page. I want to send multiple values for this drop down from controller. I have id and name in my data base each id contains five different names. Is it possible to send these multiple values to the view depending on id to controller. Plz let me know if any one have any idea.

Thanks
Posted

Yes, it is possible:

Controller:
C#
List<MyType> data= GetStuffFromLinqHere();
viewBag.DropDownSelectList = 
    new SelectList( 
        data      
        "IDPropertyName", 
        "DisplayPropertyName", 
        data.FirstOrDefaut()/*Select the first Item by default (optional)*/    ); 


View, where you want the list::
HTML
@Html.DropDownListFor( model => ViewBag.ValueYouWantTheSelectedValueToBeIn, ViewBag.DropDownSelectList)



Obviously, I can't give meaningful names as I don't know what your are doing exactly. IDPropertyName is the name of the property you want in the Id of the list item, similarly DisplayPropertyName is the name of the property you want to display in the DDL. ValueYouWantTheSelectedValueToBeIn is where you want the selected value to be placed when the form is submitted.

Additionally, you may have a view model, in which case don't used the ViewBag any you should sort the List either in or after the GetStuffFromLinqHere() method call. If you have a complex id or description, you should either create a view model to handle it, or add a property to the type the Linq contains in the list.
 
Share this answer
 
Thanks for the reply


Thanks and regards,
Bhavani.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900