I have a strongly typed view from which I want to insert some data. However when I click on the submit button, the underlying data model is posted as a parameter to the controller. But when I view the contents of the parameter, it has not been updated with the user input. Why is this?
The Controller looks like;
[HttpGet]
[Authorize(Roles = "Administrator, AdminAccounts")]
public ActionResult Add(int? id)
{
ViewData["LastPerson"] = string.Empty;
return View(new EmployeeViewModel());
}
[HttpPost]
[Authorize(Roles = "Administrator, AdminAccounts")]
public ActionResult Add(EmployeeViewModel employeeViewModel)
{
if (ModelState.IsValid)
{
ViewData["LastPerson"] = employeeViewModel.Employee.Forename + " " +
employeeViewModel.Employee.Surname;
employeeViewModel.Employee.Add();
}
return View(new EmployeeViewModel());
}
The View looks like;
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master"
Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.EmployeeViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Add
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">
<% using (Html.BeginForm("Add", "Employee")) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Add Employee</legend>
<table>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Employee.Forename) %>
</td>
<td>
<%: Html.EditorFor(model => model.Employee.Forename)%>
<%: Html.ValidationMessageFor(model => model.Employee.Forename)%>
</td>
</tr>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Employee.Surname)%>
</td>
<td>
<%: Html.EditorFor(model => model.Employee.Surname)%>
<%: Html.ValidationMessageFor(model => model.Employee.Surname)%>
</td>
</tr>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Employee.Middlenames)%>
</td>
<td>
<%: Html.EditorFor(model => model.Employee.Middlenames)%>
</td>
</tr>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Employee.EmployeeNumber)%>
</td>
<td>
<%: Html.EditorFor(model => model.Employee.EmployeeNumber)%>
<%: Html.ValidationMessageFor(model => model.Employee.EmployeeNumber)%>
</td>
</tr>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Division.DivisionId) %>
</td>
<td>
<%: Html.DropDownListFor(model => model.Division.DivisionId, Model.DivisionSelectList, "<--Select-->")%>
<%: Html.ValidationMessageFor(model => model.Division.DivisionId)%>
</td>
</tr>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Department.DepartmentId) %>
</td>
<td>
<%: Html.DropDownListFor(model => model.Department.DepartmentId, Model.DepartmentSelectList, "<--Select-->")%>
<%: Html.ValidationMessageFor(model => model.Department.DepartmentId) %>
</td>
</tr>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Employee.AnnualLeaveAllocation) %>
</td>
<td>
<%: Html.EditorFor(model => model.Employee.AnnualLeaveAllocation)%>
<%: Html.ValidationMessageFor(model => model.Employee.AnnualLeaveAllocation)%>
</td>
</tr>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Employee.ChristmasAllocation)%>
</td>
<td>
<%: Html.EditorFor(model => model.Employee.ChristmasAllocation)%>
<%: Html.ValidationMessageFor(model => model.Employee.ChristmasAllocation)%>
</td>
</tr>
<tr>
<td align="right">
<%: Html.LabelFor(model => model.Employee.EmailAddress)%>
</td>
<td>
<%: Html.EditorFor(model => model.Employee.EmailAddress)%>
<%: Html.ValidationMessageFor(model => model.Employee.EmailAddress)%>
</td>
</tr>
<tr>
<td align="center" colspan="2" style="padding-top:20px;">
<input type="submit" value="Save" /></td>
</tr>
</table>
<% if (ViewData["LastPerson"].ToString().Length > 0)
{ %>
<p>
At time <% Response.Write(DateTime.Now.ToString("T")); %>
- You have just entered <%Response.Write(ViewData["LastPerson"].ToString()); %>.
</p>
<%} %>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
The model looks like;
public class EmployeeViewModel
{
public Employee Employee;
public Department Department;
public Division Division;
public IList<Department> Departments { get; set; }
public IEnumerable<SelectListItem> DepartmentSelectList
{
get
{
return this.Departments.Select(item => new SelectListItem
{
Text = item.DepartmentName,
Value = item.DepartmentId.ToString()
});
}
}
public IList<Division> Divisions { get; set; }
public IEnumerable<SelectListItem> DivisionSelectList
{
get
{
return this.Divisions.Select(item => new SelectListItem
{
Text = item.DivisionName,
Value = item.DivisionId.ToString()
});
}
}
/// <summary>
/// EmployeeViewModel() - CONSTRUCTOR
/// Gets all of the divisions.
/// Used to populate the drop down list so that you can assign
/// an employee to a division.
/// </summary>
public EmployeeViewModel()
{
this.Employee = new Employee();
this.Departments = new List<Department>();
this.Divisions = new List<Division>();
using (SHPContainerEntities db = new SHPContainerEntities())
{
this.Divisions = db.Divisions.ToList();
}
}