Best method to add values into dropdown in mvc4
could you please tell me what is the best method to add values into dropdown either from database or code.Please give me explatnation for this?..
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Jignesh TrivediRank #16 · 50.3K reputationPosted Dec 19, 2013, 6:28 PM EST
hi,
First of all create create class which has IEnumerable type property. data { get; set; }
public class test
{
public IEnumerable
public string SelectedData { get; set; }
}
fill this in controller action method.
public ActionResult Index()();
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
test t = new test();
var newData = new List
newData.Add(new SelectListItem { Text = "Yes", Value = "Y" });
newData.Add(new SelectListItem { Text = "No", Value = "N" });
t.data = newData;
return View(t);
}
and used it on view
@{
ViewBag.Title = "Home Page";
}
@model MvcApplication2.Controllers.test
@ViewBag.Message
@Html.DropDownListFor(p => p.SelectedData, Model.data);
hope this will help you.