当我单击提交按钮时,基础模型不会更新

时间:2010-10-05 15:46:26

标签: asp.net-mvc

我有一个强类型视图,我想从中插入一些数据。但是,当我单击提交按钮时,基础数据模型将作为参数发布到控制器。但是当我查看参数的内容时,它尚未使用用户输入进行更新。 这是为什么?

控制器看起来像;

[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());
}

视图看起来像;

<%@ 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>

模型看起来像;

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();
        }
    }

1 个答案:

答案 0 :(得分:1)

当您发布到添加控制器时,您只传递一个Employee而不是EmployeeViewModel。所以你可能想尝试类似的东西:

[HttpPost]
[Authorize(Roles = "Administrator, AdminAccounts")]
public ActionResult Add(Employee employee)
{
    var employeeViewModel = new EmployeeViewModel();
    if (ModelState.IsValid)
    {
        ViewData["LastPerson"] = employee.Forename + " " + employee.Surname;
        employeeViewModel.Employee = employee;
        employeeViewModel.Employee.Add();
    }
    return View(employeeViewModel);
}