I'm using ASP.NET MVC Core, Entity Framework Core and SQL Server. I have the following Models and Controllers listed below. Everything works fine, I'm able to retrieve the data from my Database to my DropDownList Controls using fine.
What I would like to know if someone could assist me with is there a better way to retrieve the data in the "public IActionResult Create()" that I have now within the EmployeeController?
I would like if possible but not sure how to, create 2 Methods called public IActionResult DeptData() and IActionResult BldgData() to retrieve the data to those Methods and then pass the returned data to the public IActionResult Create() Method of the EmployeeController rather than how I have it now.
Employee Model:
public class Employee
{
[Key]
public int EmpId { get; set; }
[Required]
public string EmpFirstName { get; set; }
[Required]
public string EmpLastName { get; set; }
public int DeptId { get; set; }
public Department Department { get; set; }
public int BldgId { get; set; }
public Building Building { get; set; }
}
EmployeeController:
public class EmployeeController : Controller
{
private DataEntryContext _context;
public EmployeeController(DataEntryContext context)
{
_context = context;
}
// GET: /<controller>/
public IActionResult Index()
{
return View(_context.Employees.ToList());
}
public string RetrieveDept()
{
var getDeptTitle = _context.Departments.ToList();
SelectList Deptlist = new SelectList(getDeptTitle, "DeptId", "DeptTitle");
ViewBag.DeptListName = Deptlist;
return View(Create);
}
public IActionResult Create()
{
var getDeptTitle = _context.Departments.ToList();
SelectList Deptlist = new SelectList(getDeptTitle, "DeptId", "DeptTitle");
ViewBag.DeptListName = Deptlist;
var getBldgTitle = _context.Buildings.ToList();
SelectList Bldglist = new SelectList(getBldgTitle, "BldgId", "BldgName");
ViewBag.BldgListName = Bldglist;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
_context.Employees.Add(employee);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
}
Employee Create View:
<form asp-controller="employee" asp-action="Create" method="post" class="form-horizontal" role="form">
<div class="form-horizontal">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="EmpFirstName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpFirstName" class="form-control" />
<span asp-validation-for="EmpFirstName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="EmpLastName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpLastName" class="form-control" />
<span asp-validation-for="EmpLastName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="DeptId" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control"></select>
<span asp-validation-for="DeptId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="BldgId" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control"></select>
<span asp-validation-for="BldgId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>