MVC Drop Down list with entity framework

前端 未结 4 1231
闹比i
闹比i 2020-12-20 18:42

I\'ve created an MVC project using entity framework code first. My model is simply a form that gathers information.

public class Application
{
    public          


        
4条回答
  •  有刺的猬
    2020-12-20 19:25

    Very simple Code step by step

    1) In Entity Framework Class

    var productsList = (from product in dbContext.Products
                         select new ProductDTO
                         {
                           ProductId = product.ProductId,
                           ProductName = product.ProductName,
                           }).ToList();
    

    2) In Controller

    ViewBag.productsList = new EntityFrameWorkClass().GetBusinessSubCategoriesListForDD();
    

    3) In View

    @Html.DropDownList("Product_ProductId", new SelectList(ViewBag.productsList, "ProductId", "ProductName"), new { @class = "form-control" })
    

    OR

    @Html.DropDownListFor(m=>m.Product_ProductId, new SelectList(ViewBag.productsList , "ProductId", "ProductName"), new { @class = "form-control" })
    

提交回复
热议问题