Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dynamically dropdownlist in mvc (Razor)
Posted

HTML
//this is Static Dropdown Menu in MVC 3 Or 4 And also get selected value in Model

//Static DropDown For Prifix
    List<SelectListItem> list = new List<SelectListItem>();
    list.Add(new SelectListItem { Text = "Mr.", Value = "1" });
    list.Add(new SelectListItem { Text = "Mrs.", Value = "2" });
    list.Add(new SelectListItem { Text = "Miss.", Value = "3" });
    list.Add(new SelectListItem { Text = "Dr.", Value = "4" });

<div>@Html.DropDownListFor(m => m.Gender, gender)</div>
 
Share this answer
 
Follow the below steps for populating drop down in mvc razor

Step I:
Make a class in model like
C#
public class student
{
public int id {get;set;}
public string Name {get;set;}

}

public class studentModel
{
public string SelectedStudent {get;set;}
public List<student> StudentList= new List<student> 
{
new student {id=1,Name="Antariksh"},
new student {id=2,Name="Anurag"},
new student {id=3,Name="Anu"},

};
}


Step II:
In your controller code is like that:

C#
[HttpGet]
public ActionResult Index()
{
StudentModel model= new StudentModel();
return view(model);
}



Step III:

In your view
C#
@model projectName.models.StudentModel
@Html.DropDownListFor(x=>x.SelectedStudent,new SelectList(Model.StudentList, "id", "Name"), "--Please Select--")




I hope this will help you
Cheers!! :)
 
Share this answer
 
v2

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