Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC dropdown-list from database

Ok, so I'm new to this whole MVC-world, but it seems to be a pretty good way of getting things done and I'm trying to make it work here.

The problem is: I can't get data from my table in my SQL-database to a simple drop-down form on my registration page.

I have just no idea where to put the stuff, where to code to open the table, select the ids, where to put the response.write and how do I send it to the view?

My Model is this:

    public class users
{
    public string name {get; set;}
    public int user_id {get; set;}
}

My Controller is this:

    [HttpGet]
    public ActionResult ListUser()
    {
        return View();
    }

And my View is this:

@model Community.Models.users

I have googled for 2 days now and watched several videos on youtube but of no use, I can't find it. Please, anyone with some knowledge here? And please point me to some good tutorials and/or forums where I can browse for more questions I might have


Still no luck on this project..

I'm creating a form and within that form, i want a db-loop (IEnumerable).. But the current model is not a IEnumerable. I'm pretty much stuck, watched a bunch of tutorials and they all just list ONE connection, what if I want two models?

Here is my Controller, I get that you must pass a list to the view, right?

    public ActionResult Registration()
    {
        return View(db.users.ToList());
    }

How do i get hold of that list in my view witout an IEnumerable model?

@neoistheone, your example didnt help me much, my DB opens like this:

private DataBaseContext db = new DataBaseContext();

and i don't know how, but it opens the connection. I've tried for so many hours now, its just silly, haven't slept for soo long!

I'm used to programming ASP-Classic fyi, and this is my first serious try to upgrade my knowledge about programing an up-to-date language and OOP.

like image 930
Tobias Avatar asked Sep 04 '13 12:09

Tobias


People also ask

How bind dropdown from database in asp net MVC?

For that add a View by right-clicking inside ActionResult and select AddView and provide its name as Index. After adding the View add a Namespace to the Model as shown below. Here we can directly access the MobileList from the Model. Now just run the application and just check it.

How fetch data from database in DropDownList in MVC?

DropDownList = new SelectList(list, "Key", "Display"); and then finally, you need to send your model to the view: return View(model);


1 Answers

Add the SelectList to your model:

public SelectList DropDownList { get; set; }

build the class for that collection:

public class MyListTable
{
    public string Key { get; set; }
    public string Display { get; set; }
}

and then in your controller, load the data for the MyListTable class from the database:

var list = new List<MyListTable>();

using (SqlConnection c = new SqlConnection(cString))
using (SqlCommand cmd = new SqlCommand("SELECT KeyField, DisplayField FROM Table", c))
{
    using (SqlDataReader rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            list.Add(new MyListTable
            {
                Key = rdr.GetString(0),
                Display = rdr.GetString(1)
            });
        }
    }
}

var model = new users();
model.DropDownList = new SelectList(list, "Key", "Display");

and then finally, you need to send your model to the view:

return View(model);

Now in the Razor you can display this:

@Html.DropDownListFor(m => Model.DropDownList);

You of course can name these things better names, but you get the idea.

like image 171
Mike Perrenoud Avatar answered Oct 25 '22 20:10

Mike Perrenoud