DROPDOWN BIND DYNAMICALLY WITH ENTITY FRAME WORK
---------------------------------------------------
IN CSHTML
----------------------------
@Html.DropDownList("Departments", "select department");
IN CONTROLLER
---------------------------------------
[HttpGet]
public ActionResult Register()
{
TestEntities ob = new TestEntities();
ViewBag.Departments = new SelectList(ob.excels, "Id", "name");
return View();
}
note:
excels is table name.
Id and name is column name of table
----------------------------------------------------------------------------------------------------
DROPDOWN BIND STATIC
--------------------------------------
@Html.DropDownList("Departments", new List<SelectListItem>
{
new SelectListItem { Text = "IT", Value = "1", Selected=true},
new SelectListItem { Text = "HR", Value = "2"},
new SelectListItem { Text = "Payroll", Value = "3"}
}, "Select Department")
------------------------------------------------------------------------------------------------------------
WHEN U WANT FIND SELECTED VALUE FROM DATABASE
----------------------------------------------
"selectedValue" parameter.
ViewBag.Departments = new SelectList(db.Departments, "Id", "Name", "1");
No comments:
Post a Comment