加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:04:45 所属栏目:asp.Net 来源:网络整理
导读:我正在开发MVC4实体框架Application.I想要填充DropDownList,我想将Category List绑定到Dodropdown列表 IRepository Code IListCategory GetCategory(); 知识库 public IListCategory GetCategory() { return (from c in context.Categories select c).ToList
我正在开发MVC4&实体框架Application.I想要填充DropDownList,我想将Category List绑定到Dodropdown列表

IRepository Code

IList<Category> GetCategory();

知识库

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

    }

调节器

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

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

我在这里查看代码

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

我的控制器代码

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

解决方法

如何使用ViewBag?

视图

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

调节器

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读