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

我正在开发MVC4&实体框架Application.I想要填充DropDownList,我想将Category List绑定到Dodropdown列表

IRepository Code

@H_502_4@IList<Category> GetCategory();

知识库

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

调节器

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

之后我就在这里.如何将数据绑定到Dropdownlist?

在这里查看代码

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

我的控制器代码

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

解决方法

如何使用ViewBag?

视图

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

调节器

@H_502_4@public ActionResult AddProduct() { ViewBag.listofCategories = GetCategory(); return View(); }

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....