Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have three Class files relevant to three Database Tables.
Employee
Department
Designation

Department and Designation tables are related to employee table using foreign Key DeptID and DesID.

My question is,I want 'ReportingTo" field in Edit View should get populated through DropDownList item from the same employee class property "EmployeeName".

ViewBag.DeptID and ViewBag.DesID are generated automatically by EntityFramework while creating controller class and I have added ViewBag.EmpNo

Below is the code snippet what I did.and its working. but on selecting an item from the dropdownlist,value do not get saved in database,hence the output field is blank.

How Can I save the selected Item so that it could be seen on View Page ?

Thank you.

EmployeeController.cs
C#
// GET: /Employee/Edit/5
 
        public ActionResult Edit(int id)
        {
            Employee employee = db.Employees.Find(id);
            ViewBag.DeptID = new SelectList(db.Departments, "DeptID", "DeptShortDesc", employee.DeptID);
            ViewBag.DesID = new SelectList(db.Designations, "DesID", "ShortDesc", employee.DesID);
            ViewBag.EmpNo = new SelectList(db.Employees, "EmpNo", "EmployeeName", employee.EmpNo);
           
        }

        
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DeptID = new SelectList(db.Departments, "DeptID", "DeptShortDesc", employee.DeptID);
            ViewBag.DesID = new SelectList(db.Designations, "DesID", "ShortDesc", employee.DesID);
            ViewBag.EmpNo = new SelectList(db.Employees, "EmpNo", "EmployeeName", employee.EmpNo);
          
        }

Edit.cshtml file
C#
<div class="editor-label">
            @Html.LabelFor(model => model.EmpNo,"ReportingTo")
        </div>
        <div class="editor-field">
            @Html.DropDownList("EmpNo", String.Empty)
            @Html.ValidationMessageFor(model => model.EmpNo)
        </div>
Posted

1 solution

Model class:

C#
namespace Emp1_MVC.Models
{
using System;
using System.Collections.Generic;

public partial class Emp1MVC
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
}
}

controller:

C#
public class HomeController : Controller
{
//
// GET: /Home/
ASPEntities dc = new ASPEntities();
private List<SelectListItem> GetCities()
{
List<SelectListItem> cities = new List<SelectListItem>();
cities.Add(new SelectListItem { Value = "1", Text = "Delhi" });
cities.Add(new SelectListItem { Value = "2", Text = "Pune" });
cities.Add(new SelectListItem { Value = "3", Text = "Gurgaon" });
cities.Add(new SelectListItem { Value = "4", Text = "Noida" });
cities.Add(new SelectListItem { Value = "5", Text = "Bangalore" });
cities.Add(new SelectListItem { Value = "6", Text = "Chennai" });
cities.Add(new SelectListItem { Value = "7", Text = "Hyderabad" });
return cities;
}
[HttpGet]
public ActionResult Third()
{ 
ViewBag.citiesddl = GetCities();
return View();
}
[HttpPost]
public ActionResult Third(Emp1MVC emp)
{
return View(emp);
}
}

View File :

HTML
@model Emp1_MVC.Models.Emp1MVC
@{
ViewBag.Title = "Third";
}
<h2>Third</h2>
@using (Html.BeginForm())
{
@Html.Label("lblEmpID", "Employee ID");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@Html.TextBoxFor(c => c.EmpID);
<br />
<br />
@Html.Label("lblEmpName", "Employee Name");
@MvcHtmlString.Create(" ");
@Html.TextBoxFor(c => c.EmpName);
<br />
<br />
@Html.Label("lblCCity", "Employee City");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@Html.DropDownList("ddlCity", new SelectList(ViewBag.citiesddl, "Value", "Text"), "----Select City----");
<br />
<br />
<input type="submit" value="Add" />
}
 
Share this answer
 
v3
Comments
Naz_Firdouse 16-Jul-13 8:22am    
added pre tags...
saurabh kumar mahto 16-Jul-13 8:27am    
Thank you Suresh for your spontaneous reply..I am trying your solution.Hope I get my answer..
saurabh kumar mahto 16-Jul-13 8:38am    
Instead of hardcoded values ,how can i get the list items from database table column "EmployeeName"

cities.Add(new SelectListItem { Value = "1", Text = "Delhi" });
Member 13338860 7-Aug-17 3:10am    
how to add dropdownlist values to database in asp.net mvc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900