"MVC-6ren"> "MVC-大家好,我是 MVC 的新手,我找不到以下异常的解决方案。 “不存在具有键‘部门’的‘IEnumerable’类型的 ViewData 项。” 这是它的cshtml代码 @using Learning-6ren">
gpt4 book ai didi

c# - 异常 "There is no ViewData item of type ' IEnumerable"MVC

转载 作者:太空宇宙 更新时间:2023-11-03 19:10:49 29 4
gpt4 key购买 nike

大家好,我是 MVC 的新手,我找不到以下异常的解决方案。

“不存在具有键‘部门’的‘IEnumerable’类型的 ViewData 项。”

这是它的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>

Controller 代码

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 的创建操作中添加 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# - 异常 "There is no ViewData item of type ' IEnumerable<SelectListItem>"MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753779/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com