asp.net-mvc 使用 MVC 4 和实体框架填充 DropDownList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19876604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:18:46  来源:igfitidea点击:

Populate DropDownList using MVC 4 & Entity Framework

asp.net-mvcentity-frameworkasp.net-mvc-4

提问by TechGuy

I'm developing MVC4 & Entity Framework Application.I wanted to populate DropDownList,I wanted to bind Category List to Dodropdown list

我正在开发 MVC4 和实体框架应用程序。我想填充 DropDownList,我想将类别列表绑定到 Dodropdown 列表

IRepository Code

IRepository 代码

IList<Category> GetCategory();

Repository

存储库

public IList<Category> GetCategory()
    {
        return (from c in context.Categories
                select c).ToList();

    }

Controller

控制器

  public IList<Category> GetCategory()
    {
        return icategoryRepository.GetCategory();
    }

After that I stucked here.How do i bind data to Dropdownlist ?

之后我被困在这里。如何将数据绑定到 Dropdownlist ?

My View Code here

我的查看代码在这里

<label for="ProductType">Product Type</label>
       @Html.DropDownListFor(m => m.ProductType,new List<SelectListItem>)

My Controller Code

我的控制器代码

 public ActionResult AddProduct()
    {
        return View();
    }

采纳答案by Morten Anderson

How about using ViewBag?

使用 ViewBag 怎么样?

View

看法

<label for="ProductType">Product Type</label>
   @Html.DropDownListFor(m => m.ProductType,ViewBag.ListOfCategories)

Controller

控制器

public ActionResult AddProduct()
{
    ViewBag.ListOfCategories = GetCategory();
    return View();
}

回答by rochasdv

You could do this:

你可以这样做:

@Html.DropDownListFor(x => x.IdCategory, ViewBag.Categories)

But I would recommend you to avoid ViewBag/ViewData and profit from your viewmodel:

但我建议您避免使用 ViewBag/ViewData 并从您的视图模型中获利:

public ActionResult AddProduct()
{
    var model = new TestModel();

//This is just a example, but I advise you to turn your IList in a SelectListItem, for view is more easy to work. Your List Categories will be like this hardcoded:

model.ListCategories= new SelectList(new[]
{
    new { Value = "1", Text = "Category 1" },
    new { Value = "2", Text = "Category 2" },
    new { Value = "3", Text = "Category 3" },
}, "Value", "Text");

return View(model);

}

}

and in the view:

并在视图中:

@Html.DropDownListFor(x => x.IdCategory, Model.ListCategories)

I hope I have helped

我希望我有所帮助

回答by Jerad Rose

Using the ViewBag(as some have suggested in other answers/comments) to get data from your controller to view is generally seen as a code smell.

使用ViewBag(正如某些人在其他答案/评论中所建议的那样)从控制器获取数据以查看通常被视为代码异味。

Your ViewModelshould ideally contain all of the data you need for your view. So use your controller to populate this data on a property of your ViewModel:

ViewModel理想情况下,您应该包含视图所需的所有数据。因此,使用您的控制器在 ViewModel 的属性上填充此数据:

SelectList ProductTypes { get; set; }

Then bind your dropdown to this value

然后将您的下拉列表绑定到此值

@Html.DropDownListFor(m => m.ProductType, Model.ProductTypes)

You can find this same answer given on this post.

您可以在这篇文章中找到相同的答案。

回答by Saif

Very simple Code step by step 1) In Entity Framework Class

非常简单的代码一步一步 1) 在实体框架类中

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

2) In Controller

2) 在控制器中

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

3) In View

3) 在视图中

@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" })