In drop down the values are not coming. Can you please help me ?
AddEmp.cshtml
<script type="text/javascript">
$(function () {
//Remove the dummy row if data present.
if ($("#tblEmployees tr").length > 2) {
$("#tblEmployees tr:eq(1)").remove();
}
else {
var row = $("#tblEmployees tr:last-child");
row.find(".Edit").hide();
row.find(".Delete").hide();
row.find("span").html(' ');
}
});
function AppendRow(row, EMPID, EMPNAME, AGE) {
//Bind Id.
$(".EMPID", row).find("span").html(EMPID);
$(".EMPID", row).find("input").val(EMPID);
//Bind Name.
$(".EMPNAME", row).find("span").html(EMPNAME);
$(".EMPNAME", row).find("input").val(EMPNAME);
//Bind Age.
$(".AGE", row).find("span").html(AGE);
$(".AGE", row).find("input").val(AGE);
//Bind Depname.
$(".DEPNAME", row).find("span").html(DEPNAME);
$(".DEPNAME", row).find("input").val(DEPNAME);
$("#tblEmployees").append(row);
};
//Add event handler.
$(document).on("click", "#btnAdd", function () {
var txtEMPID = $("#txtEMPID");
var txtEMPNAME = $("#txtEMPNAME");
var txtAGE = $("#txtAGE");
var txtDEPNAME = $("#txtDEPNAME");
var _employee = {};
_employee.EMPID = txtEMPID.val();
_employee.EMPNAME = txtEMPNAME.val();
_employee.AGE = txtAGE.val();
_employee.DEPNAME = txtDEPNAME.val();
$.ajax({
type: "POST",
url: "/api/AjaxAPI/InsertEmp",
data: JSON.stringify(_employee),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r);
var row = $("#tblEmployees tr:last-child");
if ($("#tblEmployees tr:last-child span").eq(0).html() != " ") {
row = row.clone();
}
AppendRow(row, r.EMPID, r.EMPNAME, r.AGE, r.DEPNAME);
txtEMPID.val("");
txtEMPNAME.val("");
txtAGE.val("");
txtDEPNAME.val("");
alert("Employee Added Successfully...");
}
});
});
</script>
</head>
<body>
@Html.DropDownList("Message", (List<SelectListItem>)ViewBag.Message, "---Select---");
<input type="button" id="btnAdd" value="ADD" />
</body>
</html>
HomeController.cs
namespace jQuery_AJAX_WebAPI_MVC.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
private List<SelectListItem> GetDepartmentListSelectListItem(List<Department> dep)
{
List<SelectListItem> items = new List<SelectListItem>();
foreach (Department d in dep)
{
items.Add(new SelectListItem
{
Text = d.DEPNAME,
Value = d.DEPID.ToString()
});
}
return items;
}
public List<Department> GetDepartmentList()
{
List<Department> items = new List<Department>();
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = " SELECT DEPID,DEPNAME,MANAGER FROM Department";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new Department
{
DEPID = System.Convert.ToInt32(sdr["DEPID"]),
DEPNAME = sdr["DEPNAME"].ToString()
});
}
}
con.Close();
}
}
return items;
}
public ActionResult AddEmp()
{
EmployeeEntities2 entities = new EmployeeEntities2();
List<Employ> employees = entities.Employs.ToList();
if (employees.Count == 0)
{
employees.Add(new Employ());
}
return View(employees);
}
[HttpPost]
public ActionResult AddEmp(Employ dep)
{
List<Department> departments = GetDepartmentList();
List<SelectListItem> depitem = this.GetDepartmentListSelectListItem(departments);
var selectedItem = depitem.Find(p => p.Value == dep.DEPNAME.ToString());
if (selectedItem != null)
{
selectedItem.Selected = true;
ViewBag.Message = selectedItem.Value;
}
return View(dep);
}
}
}