Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
999 views
in Technique[技术] by (71.8m points)

asp.net mvc - MVC Drop Down list with entity framework

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()
{
    IEnumerable<SelectListItem> list = from s in db.Applications
                                       select new SelectListItem
                                       {
                                           Selected = false,
                                           Text = s.State,
                                           Value = s.State
                                       };
    return list;
}

Or this...

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...