Hello everyone
When I try this part of code in my View - It works fine with my js code
<div class="form-group">
@Html.LabelFor(model => model.PositionId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PositionId, new SelectList(" "), "--Select Position--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PositionId, "", new { @class = "text-danger" })
</div>
</div>
<script>
$(document).ready(function () {
$("#DepartmentId").change(function () {
$.get("/Insertion/GetPositionList", { DepartmentId: $("#DepartmentId").val() }, function (data) {
$("#PositionId").empty();
$.each(data, function (index, row) {
$("#PositionId").append("<option value='" + row.PositionId + "'>" + row.PositionName + "</option>")
});
});
})
});
</script>
But when I try to write in my database my result of input from my web form I have troubles and null values instead in my database
So I need to write my code like this
To have in my database values of name of selected position
<div class="form-group">
@Html.LabelFor(model => model.PositionName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PositionName, new SelectList(" "), "--Select Position--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PositionName, "", new { @class = "text-danger" })
</div>
</div>
But my dropdownlist doesnot work properly....Because there is no value of names of positions...
My controller works like this
[HttpPost]
public ActionResult Load(ViewModel model)
{
DBEntities db = new DBEntities();
Department dep = new Department();
dep.DepartmentId = model.DepartmentId;
dep.DepartmentName = model.DepartmentName;
DepPosition pos = new DepPosition();
pos.PositionId = model.PositionId;
//pos.PositionName = model.PositionName;
Gender gen = new Gender();
gen.GenderId = model.GenderId;
gen.GenderName = model.GenderName;
int latestEmpId = dep.DepartmentId;
Incentive inc = new Incentive();
inc.IncentiveAmount = model.IncentiveAmount;
inc.IncentiveId = model.IncentiveId;
Employee emp = new Employee();
emp.Name = model.Name;
emp.Gender = model.GenderName;
emp.Age = model.Age;
emp.Position = model.PositionName;
emp.Salary = model.Salary;
emp.HireDate = model.HireDate;
emp.Department_Id = model.DepartmentId;
emp.Incentive_Id = model.IncentiveId;
db.Employees.Add(emp);
db.SaveChanges();
ModelState.Clear();
ViewBag.SuccessMessage = "Информация добавлена автоматически";
return View("Load", new ViewModel());
}
{
db.Configuration.ProxyCreationEnabled = false;
List<DepPosition> PositionList = db.DepPositions.Where(x => x.Department_Id == DepartmentId).ToList();
return Json(PositionList, JsonRequestBehavior.AllowGet);
}
How it possible to write js code or controller code to have data in ddlist and have no problem writing it in database?