C# 下拉列表

C# 下拉列表,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,如何使用C#中的linq和MVC模型从SQL Server数据库填充@HTML.dropdownlist或@HTML.dropdownlistfor?我见过很多使用ViewData的示例,但我想使用model。我想我的问题是从数据库中获取数据并将其放入一个列表中,以便在视图中使用 我需要一个简单但详细的例子 谢谢 我试着给你做一个有三个演员的样板 看法 控制器 IEnumerable<YourEntity> result =

如何使用C#中的linq和MVC模型从SQL Server数据库填充@HTML.dropdownlist或@HTML.dropdownlistfor?我见过很多使用ViewData的示例,但我想使用model。我想我的问题是从数据库中获取数据并将其放入一个列表中,以便在视图中使用

我需要一个简单但详细的例子


谢谢

我试着给你做一个有三个演员的样板

看法


控制器

IEnumerable<YourEntity> result =
                            from item in GetListSample()
                            select new YourEntity
                            {
                                Text = item.Name,
                                Value = item.Value
                            };
  model.YourSource= result;
IEnumerable结果=
来自GetListSample()中的项
选择新实体
{
Text=项目名称,
值=项。值
};
model.YourSource=结果;
模型

公共类模型
{
公共IEnumerable YourSource{get;set;}
}

注意:当您创建视图时,必须将模型注入视图中,我没有sql示例,但我可以帮助您使用MVC呈现页面

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestPortal.Areas.JB.Controllers
{
    public class ExampleController : Controller
    {
        //
        // GET: /JB/Example/

        public ActionResult Index()
        {
            Models.Example.ExampleIndex output = new Models.Example.ExampleIndex();
            output.DropDownData.Add(new SelectListItem()
            {
                Text = "One",
                Value = "1"
            });

            output.DropDownData.Add(new SelectListItem()
            {
                Text = "Two",
                Value = "2"
            });


            return View(output);
        } 

    }
}
ExampleIndex.cs:注意,构建视图模型将数据传递给视图总是很好的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestPortal.Areas.JB.Models.Example
{
    public class ExampleIndex
    {
        public ExampleIndex()
        {
            this.DropDownData = new List<SelectListItem>();
        }

        public List<SelectListItem> DropDownData
        {
            get;
            set;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间TestPortal.Areas.JB.Models.Example
{
公共类示例索引
{
公共示例索引()
{
this.DropDownData=新列表();
}
公共列表下拉数据
{
得到;
设置
}
}
}
Index.cshtml:

@model TestPortal.Areas.JB.Models.Example.ExampleIndex

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@Html.RFSSelect("DropDownId", Model.DropDownData)
@model TestPortal.Areas.JB.Models.Example.ExampleIndex
@{
ViewBag.Title=“Index”;
}
指数
@RFSSelect(“DropDownId”,Model.DropDownData)

根据具体情况,您是使用EF还是oledb调用?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestPortal.Areas.JB.Models.Example
{
    public class ExampleIndex
    {
        public ExampleIndex()
        {
            this.DropDownData = new List<SelectListItem>();
        }

        public List<SelectListItem> DropDownData
        {
            get;
            set;
        }
    }
}
@model TestPortal.Areas.JB.Models.Example.ExampleIndex

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@Html.RFSSelect("DropDownId", Model.DropDownData)