Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Employee Model

namespace EmployeeManagementSystem.Models
{
public class Employee
{
public int EmployeeId { get; set; }
[Display(Name = "Employee Code")]
public string EmployeeCode { get; set; }
[Required]
[Display(Name = "Employee Name")]
public string EmployeeName { get; set; }
public string Email { get; set; }
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
[Display(Name = "Present Address")]
public string PresentAddress { get; set; }
[Display(Name = "Parmanent Address")]
public string ParmanentAddress { get; set; }
[Display(Name = "Blood Group")]
public string BloodGroup { get; set; }
[Display(Name = "Hieghest Educationan Qualification")]
public string HieghestEducationanQualification { get; set; }
public string Gender { get; set; }
public string Relagion { get; set; }
[Display(Name = "D.O.B")]
public DateTime Dob { get; set; }
[Display(Name = "Joining Date")]
public DateTime JoiningDate { get; set; }
public virtual List<EmployeeDesignation> EmployeeDesignationList { get; set; }
public virtual List<EmployeeSalary> EmployeeSalaryList { get; set; }
public virtual List<EmployeePay> PayList { get; set; }
public virtual List<Designation> DesignationList { get; set; }

}
}

Employee Controller

// GET: /Employee/Create

public ActionResult Create()
{
var bloodGroupList = new[]
{
new{Id = "01", Name = "A+"},
new{Id = "02", Name = "A-"},
new{Id = "03", Name = "B+"},
new{Id = "04", Name = "B-"},
new{Id = "05", Name = "AB"},
new{Id = "06", Name = "O+"},
new{Id = "07", Name = "O-"}
};

var heqIdList = new[]
{
new{Id = "01", Name = "PSC"},
new{Id = "02", Name = "JSC"},
new{Id = "03", Name = "HSC"},
new{Id = "04", Name = "HONS"},
new{Id = "05", Name = "Master"}
};

var genderList = new[]
{
new{Id = "01", Name = "Male"},
new{Id = "02", Name = "Female"},
new{Id = "03", Name = "Other"}
};
var relagionList = new[]
{
new{Id = "01", Name = "Islam"},
new{Id = "02", Name = "Hindu"},
new{Id = "03", Name = "Other"}
};
ViewBag.BloodGroup = new SelectList(bloodGroupList, "Id", "Name");
ViewBag.HEQ = new SelectList(heqIdList, "Id", "Name");
ViewBag.Gender = new SelectList(genderList, "Id", "Name");
ViewBag.Relagion = new SelectList(relagionList, "Id", "Name");

return View();
}

//
// POST: /Employee/Create

[HttpPost]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
db.Employees.Add(employee);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(employee);
}
Create View

@model EmployeeManagementSystem.Models.Employee

@{
ViewBag.Title = "Create";
}

Create



@using (Html.BeginForm()) {
@Html.ValidationSummary(true)


Employee

@*

@Html.LabelFor(model => model.EmployeeCode)


@Html.EditorFor(model => model.EmployeeCode)
@Html.ValidationMessageFor(model => model.EmployeeCode)
*@


@Html.LabelFor(model => model.EmployeeName)


@Html.EditorFor(model => model.EmployeeName)
@Html.ValidationMessageFor(model => model.EmployeeName)



@Html.LabelFor(model => model.Email)


@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)



@Html.LabelFor(model => model.ContactNo)


@Html.EditorFor(model => model.ContactNo)
@Html.ValidationMessageFor(model => model.ContactNo)



@Html.LabelFor(model => model.PresentAddress)


@Html.EditorFor(model => model.PresentAddress)
@Html.ValidationMessageFor(model => model.PresentAddress)



@Html.LabelFor(model => model.ParmanentAddress)


@Html.EditorFor(model => model.ParmanentAddress)
@Html.ValidationMessageFor(model => model.ParmanentAddress)



@Html.LabelFor(model => model.BloodGroup)


@Html.DropDownList("Name",(SelectList)ViewBag.BloodGroup,"--Select--")
@*@Html.EditorFor(model => model.BloodGroup)
@Html.ValidationMessageFor(model => model.BloodGroup)*@



@Html.LabelFor(model => model.HieghestEducationanQualification)


@Html.DropDownList("Name",(SelectList)ViewBag.HEQ,"--Select--")
@Html.ValidationMessageFor(model => model.HieghestEducationanQualification)



@Html.LabelFor(model => model.Gender)


@Html.DropDownList("Name",(SelectList)ViewBag.Gender,"--Select--")
@Html.ValidationMessageFor(model => model.Gender)



@Html.LabelFor(model => model.Relagion)


@Html.DropDownList("Name",(SelectList)ViewBag.Relagion,"--Select--")
@Html.ValidationMessageFor(model => model.Relagion)



@Html.LabelFor(model => model.Dob)


@Html.EditorFor(model => model.Dob)
@Html.ValidationMessageFor(model => model.Dob)



@Html.LabelFor(model => model.JoiningDate)


@Html.EditorFor(model => model.JoiningDate)
@Html.ValidationMessageFor(model => model.JoiningDate)



<input type="submit" value="Create" />



}


@Html.ActionLink("Back to List", "Index")


@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Posted

1 solution

Just add the dropdown name same as the model field name such as:
The dropdownlist for Employee Model Field "BloodGroup" as follows:
@Html.DropDownList("BloodGroup",(SelectList)ViewBag.BloodGroup,"--Select--")

 
Share this answer
 
Comments
Md. Shihab Uddin 10-Dec-14 5:36am    
Thanks

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