3 Replies - 9502 Views - Last Post: 05 November 2010 - 12:03 AM

#1 kstr  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 60
  • Joined: 19-October 09

asp.net mvc2 dropdownlist

Posted 01 August 2010 - 10:02 PM

I want to show a dropdownlist from within a .ascx

According to the example project of the music store, I am doing it like this:


<div class="editor-field">
                <%: Html.DropDownList("PlaceID",new SelectList(ViewData["Places"] as IEnumerable,
                                                                "Place",
                                                                "Town",
                                                                Model.PlaceID)) %>
</div>





Now i want the text of the items to be a combination of two properties. (Town + zip instead of Town alone)

How do I do that?

(Asp.net MVC2; .net 4)

Is This A Good Question/Topic? 0
  • +

Replies To: asp.net mvc2 dropdownlist

#2 kstr  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 60
  • Joined: 19-October 09

Re: asp.net mvc2 dropdownlist

Posted 08 August 2010 - 04:18 AM

*Bump*

Anyone please? :unsure:

Or maybe a link to a tutorial?
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: asp.net mvc2 dropdownlist

Posted 08 August 2010 - 04:58 AM

try this tutorial...

http://www.dreaminco...a-dropdownlist/
Was This Post Helpful? 1
  • +
  • -

#4 agent1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 09-July 10

Re: asp.net mvc2 dropdownlist

Posted 05 November 2010 - 12:03 AM

why not pass the dropdownlist exactly what you want it to display, easier than trying to manipulate it in the view?
create a custom object beforehand in your controller that combines town and zip into one property and then pass that to viewdata...

just a fictional example here...
var places = db.GetPlaces().ToList();

var placesForDropDown = from p in places
                        select new {Id=p.PlaceId,Place = p.Place,TownAndZip = p.Town + p.Zip };

ViewData["Places"] = placesForDropDown;




then in your partial view use:
<div class="editor-field">
	                <%: Html.DropDownList("PlaceID",new SelectList(ViewData["Places"] as IEnumerable,
	                                                                "Place",
	                                                                "TownAndZip",
	                                                                Model.PlaceID)) %>
	</div>



This post has been edited by agent1: 05 November 2010 - 12:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1