没有类型为'IEnumerable <selectlistitem>

时间:2017-02-24 15:29:39

标签: asp.net-mvc

我有关于dropdown的问题。从我的代码中显示数据库的下拉值。但是当我没有填写任何值并按下输入错误时,没有“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();
        }

Create.cshtml

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

谢谢。

1 个答案:

答案 0 :(得分:0)

当您提交表单并且您的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")