ASP.NET MVC从数据库下拉列表 [英] ASP.NET MVC dropdown-list from database

查看:256
本文介绍了ASP.NET MVC从数据库下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我是新来的这整个MVC-世界,但它似乎是做事情,我试图让在这里工作的pretty的好办法。

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.

的问题是: 我无法从我在我的SQL数据库表中的数据来对我的注册页面的简单下拉型窗体。

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.

我只是不知道往哪儿放了东西,到哪里code打开表,选择IDS,放在哪里的Response.Write 和我该如何将它发送给看法?

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?

我的模型是这样的:

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

我的控制器是这样的:

My Controller is this:

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

和我的看法是这样的:

@model Community.Models.users

我GOOGLE了2天了,看着几部影片在YouTube上,但没有用的,我找不到它。请,任何人只要有一些知识在这里?并请点我一些很好的教程和/或论坛,在那里我可以了解更多的问题,我可能有

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..

我要创建一个表单和表单内,我希望有一个DB-环路(的IEnumerable )。但目前的模式是不是的IEnumerable 。我是pretty的多卡,看了一堆教程和他们都只是列表中的一个连接,如果我想两个型号?

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

我怎么弄个该列表中的在我看来不用其他的的IEnumerable 模式?

@neoistheone,你的例子并没有帮助我很多,我的数据库打开这样的:

@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!

我已经习惯了编程ASP-经典仅供参考,这是我第一次认真尝试升级有关编程的最高最新的语言和OOP我的知识。

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.

推荐答案

添加的SelectList 到模型:

public SelectList DropDownList { get; set; }

建立类的集合:

build the class for that collection:

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

,然后在你的控制器,用于加载从数据库 MyListTable 类中的数据:

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.

这篇关于ASP.NET MVC从数据库下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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