发布到编辑表单时,DropDownList项为null

时间:2011-03-05 22:57:19

标签: c# asp.net asp.net-mvc asp.net-mvc-2

所以我有一个编辑员工的页面。

这是我的观点模型:

public class EmployeesViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int EmployeeId { get; set; }      

    [Required(ErrorMessage = "Position is required")]
    [DisplayName("Position")]
    public int EmployeeTypeId { get; set; }

    [Required(ErrorMessage = "Name is required")]
    [DisplayName("Name")]
    public string Name { get; set; }


    public IEnumerable<EmployeeType> EmployeeTypes { get; set; }
}

这是我的控制器:

public class EmployeesController : Controller
{
    public ActionResult Edit(int id)
    {
        //get employee from id
        var employee = GetEmployee(id);

        if (employee != null)
        {
            var viewModel = new EmployeesViewModel
            {
                EmployeeId = employee.EmployeeID,
                EmployeeTypeId = employee.EmployeeTypeID,
                Name = employee.Name,

                EmployeeTypes = _adminRepository.GetAllEmployeeTypes(),                    
            };

            return View(viewModel);
        }

        //if no employee exists for this id, redirect to the Create page and display a friendly message
        TempData["message"] = "No employee exists with an ID of " + id + ", you can create a new employee here.";
        return RedirectToAction("Create");
    }



    [HttpPost]
    public ActionResult Edit(EmployeesViewModel viewModel)
    {
        //if editing an employee, fetch it; otherwise, create a new one
        Employee employee = GetEmployee(viewModel.EmployeeId);
        TryUpdateModel(employee);

        if (ModelState.IsValid)
        {              
            SaveEmployee(employee);
            TempData["message"] = "Employee has been saved.";
            return RedirectToAction("Details", new { id = employee.EmployeeID });
        }
        return View(viewModel);     // validation error, so redisplay same view
    }
}

我的编辑视图页面:

<%@ Page Title="" Language="C#" MasterPageFile="/Admin.Master" Inherits="System.Web.Mvc.ViewPage<EmployeesViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <h1>Edit Employee</h1>

    <% Html.EnableClientValidation(); %>
    <% using (Html.BeginForm("Edit", "Employees", FormMethod.Post)) { %>

        <%: Html.HiddenFor(m => m.EmployeeId)%>

        <div class="editor-label"><%: Html.LabelFor(m => m.EmployeeTypeId) %></div>
        <div class="editor-field">
            <%= Html.DropDownListFor(m => m.EmployeeTypeId, new SelectList(Model.EmployeeTypes, "EmployeeTypeID", "Position", Model.EmployeeTypeId), "- Select an Employee Type -")%>
            <%: Html.ValidationMessageFor(m => m.EmployeeTypeId)%>
        </div>

        <div class="editor-label"><%: Html.LabelFor(m => m.Name) %></div>
        <div class="editor-field">
            <%: Html.TextBoxFor(m => m.Name)%>
            <%: Html.ValidationMessageFor(m => m.Name)%>
        </div>

        <p>
            <input type="submit" value="Save" />
            <%: Html.ActionLink("Cancel", "Index") %>
        </p>

    <% } %>

</asp:Content>

提交表单后,它在if (ModelState.IsValid)处失败。它会在调用return View(viewModel);时尝试重新显示视图,并收到此错误消息:

值不能为空。

参数名称:项目

&lt;%= Html.DropDownListFor(m =&gt; m.EmployeeTypeId,new SelectList(Model.EmployeeTypes,“EmployeeTypeID”,“Position”,Model.EmployeeTypeId),“ - 选择员工类型 - ” )%&GT;

我不确定为什么会这样。当我导航到页面时,下拉列表正确加载,但在重新显示视图时却没有。

有谁知道这里发生了什么?

2 个答案:

答案 0 :(得分:10)

在重新显示视图以防错误之前,您需要从存储库重新加载视图模型上的EmployeeTypes属性。永远不会发布此属性,因此在POST操作中它将始终为null,并且在呈现视图时,帮助程序将引发异常:

[HttpPost]
public ActionResult Edit(EmployeesViewModel viewModel)
{
    //if editing an employee, fetch it; otherwise, create a new one
    Employee employee = GetEmployee(viewModel.EmployeeId);
    TryUpdateModel(employee);
    if (ModelState.IsValid)
    {              
        SaveEmployee(employee);
        TempData["message"] = "Employee has been saved.";
        return RedirectToAction("Details", new { id = employee.EmployeeID });
    }

    // Reload employee types from repository before redisplaying the view
    viewModel.EmployeeTypes = _adminRepository.GetAllEmployeeTypes();

    // validation error, so redisplay same view
    return View(viewModel);
}

答案 1 :(得分:0)

EmployeesController.Edit(EmployeesViewModel viewModel)中,您需要在返回视图之前使用以下行:

 EmployeeTypes = _adminRepository.GetAllEmployeeTypes()

在您的初始EmployeesController.Edit(int id)中,您执行此操作,然后显示。您不必填充视图模型的其余部分的原因是因为它们的值是随表单一起提交的。但是,提交的EmployeeType的唯一值是所选的值。视图的值必须来自某个地方,并且您不会将它们放在该控制器操作中的任何位置。

我不建议这样做,但另一种选择是将员工类型数组(用逗号分隔)放在View上名为EmployeeTypes的隐藏输入字段中。,因为这样的值会至少要提交给模型活页夹。从数据库中获取这些值是一种更好的设计。

代码

    public ActionResult Edit(int id)
    {
        var employee = GetEmployee(id);
        if(employee == null)
        {
            TempData["message"] = "No employee exists with an ID of " + id + ", you can create a new employee here.";
            return RedirectToAction("Create");    
        }
        var viewModel = new EmployeesViewModel
        {
            EmployeeId = employee.EmployeeID,
            EmployeeTypeId = employee.EmployeeTypeID,
            Name = employee.Name
        };
        return EditEmployeeView(viewModel);
    }


    public ActionResult EditEmployeeView(EmployeesViewModel viewModel)
    {
        viewModel.EmployeeTypes = viewModel.EmployeeTypes ?? EmployeeTypes = _adminRepository.GetAllEmployeeTypes();
        return View(viewModel);
    }



    [HttpPost]
    public ActionResult Edit(EmployeesViewModel viewModel)
    {
        Employee employee = GetEmployee(viewModel.EmployeeId);
        TryUpdateModel(employee);
        if (ModelState.IsValid)
        {
            SaveEmployee(employee);
            TempData["message"] = "Employee has been saved.";
            return RedirectToAction("Details", new { id = employee.EmployeeID });
        }
        return EditEmployeeView(viewModel);
    }