Populating dropdowlist dynamically in mvc

Maheshvishnu
Posted by Maheshvishnu under ASP.NET category on | Points: 40 | Views : 1193

VIEW:


Pass viewdata attribute in dropdownlist html designing code

Html.DropDownList("CarsList", new SelectList((IEnumerable)ViewData["CarsList"], "ID", "Name"))




CONTROLLER:


first get required data from database and then pass the resultant data to view from controller using viewdata attribute


public ActionResult GetData()
{
var CarNames = from c in obj.Cars
select c;

ViewData["Carsist"] = CarNames;

return View();
}

Comments or Responses

Posted by: T.Saravanan on: 9/5/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Kindly post your code inside the code tag.
Posted by: Maheshvishnu on: 9/5/2012 Level:Starter | Status: [Member] | Points: 10
@using (Html.BeginForm("BindHRlist", "Appraisal", FormMethod.Post))
{
<div style="float: left; width: 100%">
<br />
<div>
Account:
@Html.DropDownList("CarsList", new SelectList((System.Collections.IEnumerable)ViewData["CarsList"], "Id", "Name"), new { id = "ddlCars" })
</div>
}

Login to post response