Hello everyone
I need to find Name by its id and write this id in me data table
so I have in controller
[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;
Employee emp = new Employee();
emp.Position = model.PositionName;
emp.Department_Id = model.DepartmentId;
db.Employees.Add(emp);
db.SaveChanges();
ModelState.Clear();
ViewBag.SuccessMessage = "Hello";
return View("Load", new ViewModel());
}
And I need write in db my PositionName.
The matter is that I have in my View
<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>
and
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<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>
So I have id of position only, but not PositionName
How could I write in Controller, to get PositionName by its Id?
My database with Positions looks like
SELECT TOP 1000 [PositionId]
,[PositionName]
,[Department_Id]
FROM [Sample].[dbo].[DepPosition]