Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually what I want to do is:
C#
@model Client.AddData

@for( int i=0 ;i <model.address.count;++i)>
{
  @html.dropdownlistfor(model=>model.Address[i],(Ienurable<selectlistitem>)ViewBag.Data)
}

When I do this the dropdownlist doesnot show the text of current value
i.e i want to show when model.address[i]= 2 ,then it has to show Temporary in Dropdown
how can i do this plz help me !!!!!

Thanks in Advance
Posted
Updated 10-Jun-12 7:06am
v2

1 solution

Assuming that your Address class contains ID ,City ... attribute, the code below will explain to you how to bind DropDownList easily in Asp.NET MVC application.

1. In Model folder define the Address and Address Collection class,
C#
public class Address{
    public int ID{get;set};
    public string City{get;set};
    // ...
}

public class Addresses:List<Address> {
    public Addresses() {
        this.Add(new Address() {ID=1, City="A"});
        this.Add(new Address() {ID=2, City="B"});
    }
}

2 In Contoller class that is going to consume the Address collection as follow.
C#
public ActionResult Index()
{
    Addresses addresses = new Addresses();
    ViewBag.Addresses= new SelectList(addresses.AsEnumerable(), "ID","City");
    return View();
}

3. In the View page consume the ViewBag.Addresses values as follow.
C#
@Html.DropDownList("address", (SelectList)ViewBag.Addresses, "All Addresses")
 
Share this answer
 
v2
Comments
Maciej Los 10-Jun-12 15:02pm    
Good work, my 5!
Wonde Tadesse 10-Jun-12 15:27pm    
Thanks

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