填充下拉框与DB数据 [英] Populating Drop Down Box with Db Data
问题描述
设法让现在这个工作。正如预期的那样是简单的比我使之。希望这样可以节省找人做同样的事情一段时间的未来。已修改下面的工作code code。
感谢您的帮助所有。
局部视图返回下拉:
@model Project.Models.Item @ Html.DropDownListFor(M = GT; m.CategoryId,新的SelectList(ViewBag.CategoryList,类别ID,类别名称),选择)
控制器:
[HTTPGET]
公众的ActionResult的Create()
{
ViewBag.CategoryList = db.Categorys.ToList();
ViewBag.DesignerList = db.Designers.ToList(); 返回查看();
}
产品型号:
公共类项目
{ 公共项目()
{
this.Images =新的List<图像>();
} [键]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)
[ScaffoldColumn(假)]
公众诠释ITEMID {搞定;组; } 公众诠释的CategoryId {搞定;组; }
公众诠释DesignerId {搞定;组; }
公众诠释ImageId {搞定;组; } [必需(的ErrorMessage =请输入项目名称)]
[StringLength(150,MinimumLength = 2)]
公共字符串ITEMNAME {搞定;组; }
[必需(的ErrorMessage =价格不能为负)]
[范围(0,999999.99)
公共小数ITEMPRICE {搞定;组; }
[StringLength(1000,MinimumLength = 2)]
公共字符串ItemDescription {搞定;组; }
[范围(4,22)
公众诠释ItemSize {搞定;组; } //文件被用户上传
公共HttpPostedFileBase [] {文件获得;组; } 公共虚拟分类分类{搞定;组; }
公共虚拟设计师设计{搞定;组; } 公共虚拟目录<&的OrderDetail GT;订单明细{搞定;组; }
公共虚拟列表<图像>图片{搞定;组; }
}
分类型号:
公共类分类
{ [键]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)
[ScaffoldColumn(假)]
公众诠释的CategoryId {搞定;组; } [必需(的ErrorMessage =必须提供一个类别)]
[StringLength(250,MinimumLength = 1)]
公共字符串类别名称{搞定;组; }
}
我不知道如果我错过了你的code的东西,但我看不到任何一块code的你在哪里填充ViewBag.Categories集合。
在<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor%28v=vs.118%29.aspx\"相对=nofollow>文档第二个参数是更多有SelectListItem对象的集合(IEnumerable的),比有你的实体对象的SelectList集合。这与填充下拉控制造成的问题。
这是我注意到接下来的事情是,第一个参数(前pression)是选择对象的类 - 我认为是不可能的存储只值(键)和文本选择列表中的事。你应该使用一个名为像'SelectedCategory
下面整数属性Have managed to get this working now. As expected it was simpler than I was making it. Hopefully this can save someone looking to do the same thing some time in the future. Have amended code below to the working code.
Thanks for the help all.
Partial View Returning the Drop Down:
@model Project.Models.Item
@Html.DropDownListFor(m=>m.CategoryId,new SelectList(ViewBag.CategoryList,"CategoryId","CategoryName"),"Select")
Controller:
[HttpGet]
public ActionResult Create()
{
ViewBag.CategoryList = db.Categorys.ToList();
ViewBag.DesignerList = db.Designers.ToList();
return View();
}
item Model:
public class Item
{
public Item()
{
this.Images = new List<Image>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[ScaffoldColumn(false)]
public int ItemId { get; set; }
public int CategoryId { get; set; }
public int DesignerId { get; set; }
public int ImageId { get; set; }
[Required(ErrorMessage="Please Enter the Items Name ")]
[StringLength(150,MinimumLength=2)]
public string ItemName { get; set; }
[Required(ErrorMessage = "Price Cannot be Negative ")]
[Range(0,999999.99)]
public decimal ItemPrice { get; set; }
[StringLength(1000,MinimumLength=2)]
public string ItemDescription { get; set; }
[Range(4,22)]
public int ItemSize { get; set; }
//Files Being Uploaded by the User
public HttpPostedFileBase[] Files { get; set; }
public virtual Category Category { get; set; }
public virtual Designer Designer { get; set; }
public virtual List<OrderDetail> OrderDetails { get; set; }
public virtual List<Image> Images { get; set; }
}
Category Model:
public class Category
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[ScaffoldColumn(false)]
public int CategoryId { get; set; }
[Required(ErrorMessage="Must Supply a Category")]
[StringLength(250,MinimumLength=1)]
public string CategoryName { get; set; }
}
I don't know if I'm missing something in your code but I can't see any piece of code where you are populating the ViewBag.Categories collection. In documentation second parameter is more about having Collection (IEnumerable) of SelectListItem objects than having SelectList collection of your entity objects. That is causing problem with populating the dropdown control.
Next thing that I noticed is that the first parameter (the expression) is selecting the Category object - I believe that is impossible to do with select list which stores only Value(Key) and the Text. You should use here integer property named like 'SelectedCategory'
这篇关于填充下拉框与DB数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!