Posts

Showing posts from January, 2017

How to bind Static Data to DropDownList in Asp.Net MVC with c#

@model MySample.Models.EmployeeDepartment @{ ViewData.Name="GSV" } < H1>GSV</H1> @using(Html.BeginForm()) { @Html.AntiForeignKeyToken() @Html.ValidationSummary() < div> @Html.Label("Select Employee",new {@class="from-contol"}) < div class="form-group"> @Html.DropDownListFor  (m=>m.EmployeeId,new List<SelectListItem>(){ new SelectListItem(){Text="One",Value=1}, new SelectListItem(){Text="Two",Value=2}, new SelectListItem(){Text="Three",Value=3}, new SelectListItem(){Text="Four",Value=4}, },new {@class="form-control"}) < /div> < /div> }

How to Bind Drodown data in MVC Cshtml page in c#

First u consider Employee and Department Tables DepartmentContext : -------------------- Employee  Department Employee  ------------------ Id name departmentId   Foreign key From Department table Department: -------------------- id name View Model: EmployeeDepartment ------------------------ Id name EmployeeId EmployeeNames In Controller code: --------------------------------- Public ActionResult GSVActoionMethod() { DepartmentContext db=new DepartmentContext (); var employeeDepartments=db.Department.Select(a=> new EmployeeDepartment(){ Id = a.id, name=a.name, EmployeeNames=db.Employees.Where(m=>m.departmentId   == a.id).ToList() }) return View(employeeDepartments); } In ViewPage : GSVActoionMethod.cshtml ------------------------------------------------------ @model MySample.Models.EmployeeDepartment @{ ViewData.Name="GSV" } <H1>GSV</H1> @using(Html.BeginForm()) { @Html.AntiForeignK...