模型在View MVC中没有绑定

时间:2013-12-25 13:00:34

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

我有以下视图

@using LearningMVCDAL
@using LearningMVCDAL.Classes
@model Employee

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Employee</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Gender)
        </div>
        <div class="editor-field">
            @Html.DropDownList("Gender", new List<SelectListItem>{ 
       new SelectListItem { Text="Male", Value="Male"},
       new SelectListItem { Text="Female", Value="Female"}},"Select Gender")
            @Html.ValidationMessageFor(model => model.Gender)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.City)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.City)
            @Html.ValidationMessageFor(model => model.City)
        </div>
        <div class="editor-label">
            @Html.Label("Department")
        </div>
        <div class="editor-field">
            @Html.DropDownList("Department", (SelectList)ViewBag.Departments,"Select Department")
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这是我在POST请求的控制器中的代码。

 [HttpPost]
        public ActionResult Create(Employee employeeform)
        {
            if (ModelState.IsValid)
            {
                Employee employee = new Employee();

                employee.Name = employeeform.Name;
                employee.Gender = employeeform.Gender;
                employee.City = employeeform.City;
                employee.Department.ID = employeeform.Department.ID;
                objEmployeeBusinessLayer.AddEmployee(employee);
                List<Department> departments = new List<Department>();
                departments = objDepartmentBusinessLayer.Department;
                SelectList departmentList = new SelectList(departments, "ID", "Name");
                ViewBag.Departments = departmentList;
            }
            return View();
        }

使用上面的代码,我可以获得Name,Gender和City,但不能获得Department,因为它是一个类对象。请帮我将属性绑定到Department Property。

2 个答案:

答案 0 :(得分:2)

尝试将DropDownList的Employee类中的属性添加为选定的departmentId,如下所示:

public class Employee{
  public int selectedDepartmentId {get;set;}
  // Other properties
}
@Html.DropDownList("selectedDepartmentId",(SelectList)ViewBag.Departments,"Select Department",String.Empty)

或者您可以更改视图中的DropDownList,如下所示:

@Html.DropDownList("Department.DepartmentId" , (SelectList)ViewBag.Departments,"Select Department",String.Empty)

答案 1 :(得分:1)

更改为:

@Html.DropDownList("DepartmentId", (SelectList)ViewBag.Departments,"Select Department")

public class Employee
    {
        ...
        public string DepartmentId { get; set; } // now binding
        public Department Department { get; set; }
    }

因为您无法将所选值从下拉列表绑定到Custom类。

[HttpPost]
public ActionResult Create(Employee employeeform)
{
    if (ModelState.IsValid)
    {
        ...
        Employee employee = new Employee();
        employee.DepartmentID = employeeform.DepartmentId;
        ...

    }
        return View();
 }