2013-05-09 39 views
0

我想创建一个以单个下拉列表开头的索引页面。一旦用户选择了一个类别,第二个下拉列表将出现(通过Ajax调用),这将允许用户选择要编辑的模型项目。在我的控制器和视图下方使用的代码,但是,我收到以下错误使用单个下拉列表创建索引

The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[Monet.Models.DropDownValues]', but this dictionary requires a model item of type 'Monet.Models.DropDownValues'.

控制器

public ActionResult Index() 
    { 
     //redirect if security is not met. 
     if (!Security.IsAdmin(User)) return RedirectToAction("Message", "Home", new { id = 1 }); 

     var dropDownValues = (from b in db.DropDownValues 
           orderby b.Model 
           select b.Model).Distinct(); 

     ViewBag.CategoryOptions = new SelectList(dropDownValues, "Model", "Model"); 

     return View(db.DropDownValues.ToList()); 
    } 

查看

@model Monet.Models.DropDownValues 

@{ 
    ViewBag.Title = "Monet Administration"; 
} 

<h2>Monet Administration</h2> 
Update values for drop down boxes 

<div> 
    <span style="float: left;"> 
     <div class="editor-label"> 
     @Html.LabelFor(model => model.Model) 
     </div> 
     <div class="editor-field"> 
     @Html.DropDownList("Categories", (SelectList)ViewBag.CategoryOptions, "") 
     @Html.ValidationMessageFor(model => model.Model) 
     </div> 
    </span> 
    <div style="clear: both;"></div> 
</div> 

回答

1

您查看预计一个Monet.Models.DropDownValues的实例和你Ë传递一个List<Monet.Models.DropDownValues>

您应该从控制器通过单个项目(如果是有道理的):

return View(db.DropDownValues.ToList().First()); 

或更改您的视图模型类型:

@model List<Monet.Models.DropDownValues>