HTML.select (UI helper) how to build a select list with value not equal to the display value

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
geo_h
5/23/2008 8:19:44 AM
asp.net.mvc 4300 articles. 0 followers. Follow

4 Replies
824 Views

Similar Articles

[PageSpeed] 11

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
jbardrof
5/23/2008 12:43:20 PM

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
geo_h
5/24/2008 1:38:17 AM
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 = ViewData[
"CountryOfResidenceObjList"] as List<Controllers.CountryOfResidenceObj>;

"<%=Html.Select("dropDown", CountryOfResidenceObjList, "_CountryString", "_CountryValue")%>"

Is there anyway I can make it simplier for the view?

0
geo_h
5/26/2008 9:14:35 AM

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
jbardrof
5/27/2008 12:48:35 PM
Reply: