简体   繁体   中英

Populating Drop Down Box with Db Data

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'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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