Populating Drop Down Box with Db Data

泄露秘密 提交于 2019-12-11 22:13:45

问题


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; }
}

回答1:


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'




回答2:


check this code it should be

@Html.DropDownListFor(model=>model.Category.CategoryName,ViewBag.Categories as SelectList,"-- Select Category--")

also in controller set ViewBag.Categories with your db values.



来源:https://stackoverflow.com/questions/24600581/populating-drop-down-box-with-db-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!