微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在开发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();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐