带有实体框架的MVC下拉列表 [英] MVC Drop Down list with entity framework

查看:16
本文介绍了带有实体框架的MVC下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先使用实体​​框架代码创建了一个 MVC 项目.我的模型只是一个收集信息的表格.

I've created an MVC project using entity framework code first. My model is simply a form that gathers information.

public class Application
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string MiddleInitial { get; set; }
    public string LastName { get; set; }
    public int SSN { get; set; }
    public DateTime DOB { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State {get; set; }
    public string Zip { get; set; }
    public int HomePhone { get; set; }
    public int BusinessPhone { get; set; }
    public int MobilePhone { get; set; }

}

我的目标是创建一个包含所有状态的下拉列表,但我发现这非常困难,因为我已经通过带有视图和控制器的脚手架创建了数据库.有没有一种简单的方法可以做到这一点并将其与现有数据库联系起来?我已经搜索了几乎一整天都没有运气.对模型/控制器/视图要包含的内容的概述/解释会很棒!

My goal is to create a drop down list with all of the states, but I'm finding this to be very difficult given that I've already created the database via scaffolding with views and controllers. Is there a simple way to do this and tie it in with the existing database? I've searched for almost the entire day with no luck. An overview/explanation of what to include for the model/controller/view would be amazing!

更新:我创建了一个名为State"的新模型,其属性为Id"和StateName",并在数据库中创建了一些状态.在我的 Create action 方法内的应用程序"控制器中,我有:

Update: I've created a new model named "State" with properties "Id" and "StateName" and have created some states in the database. In my "Application" controller inside the Create action method I have:

控制器

public ActionResult Create()
    {

        ApplicationDbContext db = new ApplicationDbContext();
        this.ViewData["Id"] = new SelectList(db.States.ToList(), "Id", "StateName");
        return View();
    }

查看

@Html.DropDownList("Id")

现在的问题是我收到此错误没有具有键 'Id' 的‘IEnumerable’类型的 ViewData 项."非常感谢帮助!

Now the problem is I'm getting this error " There is no ViewData item of type 'IEnumerable' that has the key 'Id'." Would really appreciate help!

推荐答案

很简单.将 IEnumerable 属性添加到您的模型中(这里我建议您制作一个可以与 Application 具有完全相同属性的 ViewModel,并将以下代码作为属性包含在内).然后你只需要构建列表并将其发送到你的视图

Its quite simple. Add an IEnumerable<SelectListItem> property to your model(Here I suggest you make a ViewModel that can have the exact same properties as Application with the below code included as a property). Then you just need to build the list and send it to your view

public IEnumerable<SelectListItem> States{ get; set; }

我假设您想从数据库中检索 State 值.以下是您的操作方法:

I will assume you want to retrieve the State values from the db. Here is how you will do it:

private IEnumerable<SelectListItem> GetAllStates()
{
    IEnumerable<SelectListItem> list = from s in db.Applications
                                       select new SelectListItem
                                       {
                                           Selected = false,
                                           Text = s.State,
                                           Value = s.State
                                       };
    return list;
}

或者这个...

private IEnumerable<SelectListItem> GetAllStates()
{
    IEnumerable<SelectListItem> list = db.Applications.Select(s => new SelectListItem
                                       {
                                           Selected = false,
                                           Text = s.State,
                                           Value = s.State
                                       });

    return list;
}

然后在你的动作中做这样的事情:

Then do something like this in your action:

var app = new Application
{
    States = GetAllStates()
};

return View(app);

最后,在视图上使用 Razor 来显示这样的下拉列表

Then finally, use Razor on the view to display the Dropdown list like this

@Html.DropDownListFor(m => m.State, Model.States, "--Select a State--")

第一个参数是要更新的模型的属性,第二个是数据列表,第三个是将显示的默认消息

The 1st parameter is the property of the model to update, the 2nd is the list of data, and 3rd is the default message that will be displayed

希望这会有所帮助.

这篇关于带有实体框架的MVC下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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