填充使用MVC 4和DropDownList的;实体框架 [英] Populate DropDownList using MVC 4 & Entity Framework

查看:132
本文介绍了填充使用MVC 4和DropDownList的;实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发MVC4&安培;实体框架Application.I要填充的DropDownList,我想绑定类别列表中Dodropdown列表

IRepository code

 的IList<类别及GT;的getCategory();

 公开的IList<类别及GT;的getCategory()
    {
        返回(从context.CategoriesÇ
                选择C).ToList();    }

控制器

 公开的IList<类别及GT;的getCategory()
    {
        返回icategoryRepository.GetCategory();
    }

在我stucked here.How做我将数据绑定到DROPDOWNLIST?

我查看code此处

 <标签=ProductType>产品类型< /标签>
       @ Html.DropDownListFor(M = GT; m.ProductType,新的List< SelectListItem>)

我的控制器code

 公众的ActionResult addProduct命令()
    {
        返回查看();
    }


解决方案

如何使用ViewBag?

查看

 <标签=ProductType>产品类型< /标签>
   @ Html.DropDownListFor(M = GT; m.ProductType,ViewBag.ListOfCategories)

控制器

 公众的ActionResult addProduct命令()
{
    ViewBag.ListOfCategories =的getCategory();
    返回查看();
}

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

IRepository Code

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 ?

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();
    }

解决方案

How about using ViewBag?

View

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

Controller

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

这篇关于填充使用MVC 4和DropDownList的;实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆