【问题标题】:Exception "There is no ViewData item of type 'IEnumerable<SelectListItem>" MVC异常“没有 'IEnumerable<SelectListItem> 类型的 ViewData 项”MVC
【发布时间】:2014-01-12 06:38:01
【问题描述】:

大家好,我是 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”创建函数被调用时,我收到了这个错误。 提前致谢。

【问题讨论】:

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


    【解决方案1】:

    尝试在使用 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();
            }
    

    【讨论】:

    • 谢谢林。你拯救了我的一天。你能指导我一本好书或一系列教程,我可以从中学习 MVC 的东西。
    • 我个人喜欢在这里学习asp.net MVC:asp.net,但是如果你想看书的选择,我推荐这本书:amazon.com/Pro-ASP-NET-MVC-Adam-Freeman/dp/1430242361
    • 谢谢,我总是收到null,但这对我有帮助。
    猜你喜欢
    • 2016-05-11
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多