2012-04-08 63 views
1

在我的控制,我有:的DropDownList和ViewData的

 Category x = new Category(1, "one", 0); 
     Category y = new Category(2, "two", 1); 
     Category z = new Category(3, "three", 1); 
     List<Category> categories = new List<Category>(); 
     categories.Add(x); 
     categories.Add(y); 
     categories.Add(z); 
     ViewData["categories"] = categories; 

在我看来,我有:

<%= Html.DropDownList("categories")%> 

但我有一个错误:具有关键“类别”的ViewData的产品类型'System.Collections.Generic.List`1 [[Category,MvcApplication1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]',但必须是'IEnumerable'类型。

uggggghhh该如何解决?

+0

我的教程解释此http ://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc http://blogs.msdn .com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT 2012-04-09 02:36:28

回答

3

你可以也许尝试

ViewData["categories"] = new SelectList(categories, "Id", "Name"); 

(假设范畴有一个名字和和id字段)

然后你就可以添加逻辑来keed选择的值。

编辑:使你的错误信息是不完整的,如果我没有错:它要求一个IEnumerable<SelectListItem>

0

在行创建ViewData项目之前,你可以转换List<T>IEnumerable<T>

IEnumerable<Category> cat = categories; 
ViewData["categories"] = cat; 
+0

发生同样的错误。 – 2012-04-08 18:10:58

0

更好的方法是为每个视图/页面创建一个视图模型,用数据填充它(如果需要)并将其返回给视图/页面。切勿将您的域模型返回到视图/页面。

下面提供的代码是ASP.NET MVC3,但它很容易与您的情况,请不要向下投我的答案:)

让我们假设你正在创建一个需要新产品在一个类别中(显示在选择列表中),所以你需要一个创建视图/页面和操作方法。我将创建以下视图模型:

public class ProductCreateViewModel 
{ 
    // Include other properties if needed, these are just for demo purposes 

    public string Name { get; set; } 
    public string SKU { get; set; } 
    public string LongDescription { get; set; } 

    // This is the unique identifier of your category, 
    // i.e. foreign key in your product table 
    public int CategoryId { get; set; } 
    // This is a list of all your categories populated from your category table 
    public IEnumerable<Category> Categories { get; set; } 
} 

分类等级:

public class Category 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

在您创建视图,您将有以下:

@model MyProject.ViewModels.ProductCreateViewModel 

@using (Html.BeginForm()) 
{ 
    <table> 
      <tr> 
       <td><b>Category:</b></td> 
       <td> 
        @Html.DropDownListFor(x => x.CategoryId, 
         new SelectList(Model.Categories, "Id", "Name", Model.CategoryId), 
         "-- Select --" 
        ) 
        @Html.ValidationMessageFor(x => x.CategoryId) 
       </td> 
      </tr> 
    </table> 

    <!-- Add other HTML controls if required and your submit button --> 
} 

你创建的操作方法:

public ActionResult Create() 
{ 
    ProductCreateViewModel viewModel = new ProductCreateViewModel 
    { 
      // Here you do a database call to populate your dropdown 
      Categories = categoryService.GetAllCategories() 
    }; 

    return View(viewModel); 
} 

我好这有助于。

0

使用jquery Ajax + JSON来填充下拉菜单。这将导致更好的响应式网页。

它很简单。你所要做的就是添加一个返回类型JSONResult的新动作,并将下拉加载代码移动到那里。

在控制器类: 创建一个新的动作:

public JsonResult FillDropDown() 
     { 
      Category x = new Category(1, "one", 0); 
      Category y = new Category(2, "two", 1); 
      Category z = new Category(3, "three", 1); 
      List<Category> categories = new List<Category>(); 
      categories.Add(x); 
      categories.Add(y); 
      categories.Add(z); 

      return Json(categories,JsonRequestBehavior.AllowGet); 
     } 

在查看页:负载数据添加jQuery代码从服务器。

<script type="text/javascript"> 
    $(document).ready(function() 
    { 
     var _selbxTest = $('#selbxTest'); //Get dropdownlistbox 
     $.getJSON(
      '@Url.Action("FillDropDown","Home")', //Provide JsonResultActionName, ControllerName 
      {}, 
       function (_recdData) { 
        //Update dropdownListBox here 
        _selbxTest.append($('<option></option>').val(0).html('Select')); 
        $.each(_recdData, function (key, value) { 
         _selbxTest.append($('<option></option>').val(value.value).html(value.text)); 
        }); 
       }); 
    }); 
</script> 

最后在视图页面,而不是Html.DropDownList()

<div> 
     <select id="selbxTest"></select> 
</div> 

希望这将帮助你增加一个简单的选择框。谢谢。

0

我知道这是旧的,但万一有人正在寻找的答案

假设分类有和ID以及名称和当前的CategoryId在模型

@Html.DropDownListFor(m => m.CategoryId, new SelectList((IEnumerable)ViewData["categories"], "Id", "Name"))