大家好我是MVC的新手,我无法找到以下异常的解决方案。
“没有类型为'IEnumerable'的ViewData项目具有键'Department'。”
这是它的cshtml代码
@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", (IEnumerable<SelectListItem>)ViewBag.Departments,"Select Department")
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
控制器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LearningMVCBLL;
using LearningMVCDAL;
using LearningMVCDAL.Classes;
namespace LearningMVC.Controllers
{
public class EmployeeController : Controller
{
#region ObjectDeclartions
EmployeeBusinessLayer objEmployeeBusinessLayer = new EmployeeBusinessLayer();
DepartmentBusinessLayer objDepartmentBusinessLayer = new DepartmentBusinessLayer();
#endregion
public ActionResult Index()
{
List<Employee> employees = objEmployeeBusinessLayer.Employee;
return View(employees);
}
[HttpGet]
public ActionResult Create()
{
List<Department> departments = new List<Department>();
departments = objDepartmentBusinessLayer.Department;
SelectList departmentList = new SelectList(departments, "ID", "Name");
ViewBag.Departments = departmentList;
return View();
}
[HttpPost]
public ActionResult Create(FormCollection formCollection)
{
if (ModelState.IsValid)
{
foreach (string key in formCollection.AllKeys)
{
Response.Write("Key = " + key + " ");
Response.Write("Value = " + formCollection[key]);
Response.Write("<br/>");
}
}
return View();
}
}
}
我更改了它,但仍然遇到相同的错误。发布数据“ HttpPost”的“创建函数”被调用时出现此错误。
提前致谢。
最佳答案
尝试在使用Post HttpMethod的Create操作中添加ViewBag.Departments
,如下所示:
[HttpPost]
public ActionResult Create(FormCollection formCollection)
{
if (ModelState.IsValid)
{
foreach (string key in formCollection.AllKeys)
{
Response.Write("Key = " + key + " ");
Response.Write("Value = " + formCollection[key]);
Response.Write("<br/>");
}
}
List<Department> departments = new List<Department>();
departments = objDepartmentBusinessLayer.Department;
SelectList departmentList = new SelectList(departments, "ID", "Name");
ViewBag.Departments = departmentList;
return View();
}
关于c# - 异常“没有类型为'IEnumerable <SelectListItem>的ViewData项” MVC,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20753779/