Hi Guys, I have a newbie question on HTML.select (UI helper).
One of the tutorial I have come across shows the following:
"string [] SelectList = new string[]{'Apple', 'Orange','Mango','Banana'};"
"Html.Select('FruitList',SelectList)"
it will give me:
value = "Apple" "Apple "
However, I would like to have the following instead:
value = "1" "Apple "
What should I do on the VIEW in order to achieve this?
thx, George
![]() |
0 |
![]() |
what you need to do is create a collection to use for your data source. for example create this:
class DropDownData
{
int ID;
string Value;
}
List<DropDownData> datasource = new List<DropDownData>();Populate your list and in the html:
Html.Select("dropDown", datasource, "Value", "ID")
that'll give you the ID and Text that you are looking for.
Remember if you have gotten your answer to mark your thread as answered.
![]() |
0 |
![]() |
Thx a lot.
But when I tried to do things like:
ViewData["DropDownDataList"] = datasource
I couldn't figure out how to get it back out from the ViewData to the HTML.Select though
thx
![]() |
0 |
![]() |
Hey thx a lot,
IT WORKS
But now I have a lot of server-side code on the view:
create a List<obj>
List<Controllers.CountryOfResidenceObj> CountryOfResidenceObjList = new List<Controllers.CountryOfResidenceObj>();then, bind it from the ViewData
"CountryOfResidenceObjList"] as List<Controllers.CountryOfResidenceObj>;
CountryOfResidenceObjList = ViewData[
"<%=Html.Select("dropDown", CountryOfResidenceObjList, "_CountryString", "_CountryValue")%>"
Is there anyway I can make it simplier for the view?
![]() |
0 |
![]() |
What you can do and probably should do to make it more concise is this. give your view a viewdata for example:
ViewPage<List<Controllers.CountryOfResidenceObj>>
Then your controller is what gets the list and when you render your view:
RenderView("view.aspx", CountryOfResidinceList);
Now you will have a strongly typed view data. you then don't need to do any binding in the view codebehind.
Remember if you have gotten your answer to mark your thread as answered.
![]() |
0 |
![]() |