简体   繁体   English

带有模型的asp.net mvc 4下拉列表不起作用

[英]asp.net mvc 4 dropdown list with model isn't working

I'm converting an asp.net forms site to MVC and am having an issue with a dropdown list.我正在将一个 asp.net 表单站点转换为 MVC,但下拉列表出现问题。

Model extract:模型提取:
namespace adminLte.Models命名空间 adminLite.Models
//then further down //然后再往下

[Required(ErrorMessage = "Player Segment Required")]  
[Display(Name = "Player Segment")]  
public string Segment { get; set; }  
public IEnumerable<SelectListItem> PlayerSegmentList { get; set; } 

//model class List //模型类列表

public IEnumerable<SelectListItem> getPlayerSegmentList()  
{  
  List<SelectListItem> myList = new List<SelectListItem>();  
  var data = new[]{  
   new SelectListItem{ Value="&amp;Segment=1000",Text="1,000+"},  
   new SelectListItem{ Value="&amp;Segment=750-999",Text="750-999"},  
   new SelectListItem{ Value="&amp;Segment=500-749",Text="500-749"},  
   new SelectListItem{ Value="&amp;Segment=400-499",Text="400-499"}
  };
  myList = data.ToList();
  return myList;
}  

controller:控制器:
//?? //?? not sure about this...不确定这个...

playerExtract objModel = new playerExtract();
objModel.PlayerSegmentList = objModel.getPlayerSegmentList();
ViewBag.pSeg = new SelectList(objModel, "SegmentID", "Segment"); 

*HTML form*  
@Html.LabelFor(model => model.Segment, htmlAttributes: new {  @class = "control-label col-md-2" })  
@Html.DropDownListFor( model => model.Segment, ViewBag.pSeg as SelectList  , new { htmlAttributes = new { @id = "Segment", @class = "form-control" } })  
@Html.ValidationMessageFor(model => model.Segment, "", new { @class = "text-danger" })  

//Results compile error //结果编译错误

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Segment'.  

I'm new to MVC and am missing something - Any assistance provided is greatly appreciated!我是 MVC 的新手,我遗漏了一些东西 - 非常感谢提供的任何帮助!

You should use Model.PlayerSegmentList as your source collection used to build the select options.您应该使用Model.PlayerSegmentList作为用于构建选择选项的源集合。 So pass that as the second parameter of your Html.DropDownListFor method call.因此,将其作为Html.DropDownListFor方法调用的第二个参数传递。

@Html.DropDownListFor(model => model.Segment, Model.PlayerSegmentList,
                 new { htmlAttributes = new { @id = "Segment", @class = "form-control" } })

Also in your controller code, this line does not make any sense!同样在您的控制器代码中,这一行没有任何意义!

ViewBag.pSeg = new SelectList(objModel, "SegmentID", "Segment"); 

The first parameter of the SelectList has to be a collection, not your class object! SelectList的第一个参数必须是一个集合,而不是您的类对象! That should give you a compile time error!.那应该会给你一个编译时错误!。 Just remove that line since you already have the data you needed in your PlayerSegmentList property只需删除该行,因为您已经在PlayerSegmentList属性中拥有所需的数据

So your cleaned up controller code would be所以你清理的控制器代码将是

public ActionResult Create()
{
  var vm = new YourVieWModel();
  var objModel = new playerExtract();
  vm.PlayerSegmentList = objModel.getPlayerSegmentList();
  return View(vm); 
}

That should work assuming you are view is strongly typed tot he same YourViewModel假设您的视图是同一个YourViewModel强类型的,那应该可行

I would also recommend using P ascal C asing for C# class names/method names :)我还建议对 C# 类名/方法名使用P ascal C asing :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM