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

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

问题描述

我已经使用实体框架代码创建了一个MVC项目。我的模型只是一个收集信息的表单。

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

}



我的目标是创建一个所有状态的下拉列表,但是我发现这是非常困难的,因为我已经通过脚手架创建数据库的视图和控制器。有没有一个简单的方法来做到这一点,并与现有的数据库绑定?我已经搜索几乎整天,没有运气。对于模型/控制器/视图包含的内容的概述/解释将是惊人的!






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



控制器

  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项目。真的很感激帮助!

解决方案

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

  public IEnumerable< SelectListItem>国家{get;组; } 

我将假设您要检索状态值。以下是您将如何做:

  private IEnumerable< SelectListItem> GetAllStates()
{
var list = new Application();
IEnumerable< SelectListItem> slItem = from s in db.Applications
select new SelectListItem
{
Selected = s.Id.ToString()==Active,
Text = s.State,
Value = s.State.ToString()
};
return slItem;
}

然后在您的操作中执行此操作:

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

return View(app);

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

  @ Html.DropDownListFor(m => m.State,Model.States, - 选择状态 - )

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



希望这有帮助。


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!


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:

Controller

public ActionResult Create()
    {

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

View

@Html.DropDownList("Id")

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!

解决方案

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

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

private IEnumerable<SelectListItem> GetAllStates()
    {
        var list = new Application();
        IEnumerable<SelectListItem> slItem = from s in db.Applications
                                             select new SelectListItem
                                             {
                                                 Selected = s.Id.ToString() == "Active",
                                                 Text = s.State,
                                                 Value = s.State.ToString()
                                             };
        return slItem;
    }

Then do something like this in your action:

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

        return View(app);

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

Hope this helps.

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

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