【问题标题】:There is no ViewData item of type 'IEnumerable<SelectListItem>没有 'IEnumerable<SelectListItem> 类型的 ViewData 项
【发布时间】:2017-07-15 12:10:33
【问题描述】:

我有一个关于下拉的问题。从我的代码中,数据库中的下拉值会显示出来。但是,当我没有填写任何值并按 Enter 时,出现“没有类型为 'IEnumerable 显示的 ViewData 项”的错误。

以下是我的代码:

Category.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace BusinessLayer
{

    public interface ICategory
    {
         int CategoryId { get; set; }
         string CategoryDescription { get; set; }

    }
    public class Category:ICategory
    {
         [Required]
        public int CategoryId { get; set; }
       [Required]
        public string CategoryName { get; set; }
        [Required]
        public string CategoryDescription { get; set; }
        [Required]
        public SelectList CategoryList { get; set; }  
    }
}

CategoryBusinessLayer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLayer
{
   public class CategoryBusinessLayer
    {
        public IEnumerable<Category> Categories
        {
            get
            {
                string connectionString =
                    ConfigurationManager.ConnectionStrings["DBCSS"].ConnectionString;

                List<Category> categoryList = new List<Category>();

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand("spGetAllCategory", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        Category categories = new Category();
                        categories.CategoryId = Convert.ToInt32(rdr["CategoryId"]);
                        categories.CategoryName = rdr["CategoryName"].ToString();
                        categories.CategoryDescription = rdr["CategoryDescription"].ToString();
                        categoryList.Add(categories);
                    }
                }

                return categoryList;
            }
        }
}

ProductController.cs

 [HttpGet]
        [ActionName("Create")]
        public ActionResult Create_Get()
        {
            Category category = new Category();
            CategoryBusinessLayer cat = new CategoryBusinessLayer();
            category.CategoryList = new SelectList(cat.Categories, "CategoryId", "CategoryName");
            ViewBag.category = category.CategoryList;
            return View();
        }

创建.cshtml

  @Html.DropDownList("CategoryId", (IEnumerable<SelectListItem>)ViewBag.category as SelectList, "Select Category")

谢谢。

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    这通常发生在您提交表单并且您的 HttpPost 操作方法返回相同视图而不重新加载构建 SELECT 元素所需的 ViewData 项时。

    在您的 GET 和 POST 操作中,您可以简单地将 SelectListItem 列表设置为 ViewBag/ViewData。此外,您当前的代码不必要地进行了两次强制转换!在这个表达式的视图中

    (IEnumerable<SelectListItem>)ViewBag.category as SelectList
    

    确保在返回相同视图之前重新加载此数据。

    [HttpPost]
    public ActionResult Create(SomeViewModel model)
    {
    
       // your existing code to get category 
       ViewBag.category = category.CategoryList
                                  .Select(x=> new SelectListItem { Value=x.Id,
                                                         Text= x.CategoryName }).ToList();
       return View(model);
    }
    

    现在在你看来,

    @Html.DropDownList("CategoryId", ViewBag.category as List<SelectListItem>,
                                                                           "Select Category")
    

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 2016-05-11
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多