I'm new to MVC and trying to build an application which have departments and for each department there are employees listed under that department !
so my question is HOW do I set a default value Model.Count() in the View page
here's my code (Please note that when I loop in Model and if there's NO employee in that particular department the Model.Count() is always 0 and ActionLink is not appearing in the page ! so I need to set a default value for the Model, if you wondering why I'm using such implementation for the ActionLink that because I want to pass the current DepartmentId to the Create View and populate it to the @Html.HiddenFor for the employee Department automatically )
Any help would be appreciated
<p>
@Model.Count() = 1;
@foreach (Employee employee in Model)
{
@Html.ActionLink("Create New", "Create", new { id = employee.DepartmentId })
if (!employee.Equals(1))
{
break;
}
}
</p>
My Controller
[HttpGet]
[ActionName("Create")]
public ActionResult Create_Get(int id)
{
Employee employee = new Employee();
employee.DepartmentId = id;
return View(employee);
}
[HttpPost]
[ActionName("Create")]
public ActionResult Create_Post()
{
Employee employee = new Employee();
TryUpdateModel(employee);
if (ModelState.IsValid)
{
EmployeeContext employeeContext = new EmployeeContext();
employeeContext.AddEmployee(employee);
return RedirectToAction("Index","Employee", new { id = employee.DepartmentId });
}
return View();
}