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?..
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Dec 19, 2013, 11:28 PM
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.