Asp.net Mvc 5 DropDownList using Database with Entity Framework using strongly binding
Controller.cs
EmpireEntities ee = new EmpireEntities();
public ActionResult create()
{
List<employee> emplist = ee.employees.ToList();
ViewBag.Ename = new SelectList(emplist, "eid", "ename");
return View();
}
[HttpPost]
public ActionResult create(employee e)
{
try
{
ee.employees.Add(e);
ee.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception)
{
return View();
}
}
creta.cshtml
@Html.DropDownListFor(model => model.ename, ViewBag.Ename as SelectList, "-------Select List---------", new { @class = "form-control" })
No comments: