ASP.NET MVC的下拉列表 [英] ASP.NET MVC Drop Down List

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

问题描述

我与ASP.NET MVC一个新手,并试图绑定一个下拉列表从数据库数据。我在做电脑包计算器应用程序,基本上用户可以选择他/她想要成为从下拉列表中他/她的电脑包组件,然后订单发送到电子邮件。组件必须来自数据库。

I'm a newbie with ASP.NET MVC and trying to bind a Drop Down List with a data from a database. I'm making a computer packet calculator application, basically the users can select components he/she wants into his/her computer packet from a drop down list and then send the order into a email. The components must come from a database.

我不熟悉的MVC模式,所以我还没有完全明白在哪个文件夹我应该把应用程序的哪一部分。现在我有

I'm not familiar with the MVC model so I haven't quite understood in which folder should I put which part of the application. Right now I have

-Controllers:

- HomeController的

--HomeController

-Models

- HomeRepository

--HomeRepository

- IHomeRepository

--IHomeRepository

- Database.dbml(现在我只用了一个名为产品表和信息,我需要从有

--Database.dbml (right now I only use a table called product and the information that I need from there is

PRODUCT_DESCRIPTION和PRODUCT_PRICE)

product_description and product_price)

-views

- 首页

----指数

---- ...等...

---- ... etc ...

我设法得到产品表中的所有产品进入项目符号列表,并在索引页显示它。所以,我HomeRepository使得从Database.dbml一个DataContext。还有一个公共的IList ListAll()方法(?),其中搜索句话被写入。 IHomeRepository只有

I have managed to get all products from products table into a bulleted list and show it at Index page. So, my HomeRepository makes a datacontext from Database.dbml. There is also a public IList ListAll() method (?) where the search sentence is written. IHomeRepository has only

public interface IHomeRepository
{
  IList<product> ListAll();
}

不知怎的,它的工作原理,有一段我很高兴。我试图填充像这样的索引页下拉列表:

Somehow it works and for a while I was very happy. I tried to populate a Drop Down List in Index page like this:

    <% foreach (product m in (IENumerable)ViewData.Model
{
   Html.DropDownList("processor"m new[] {
   new SelectedListItem { Text = m.product_description, Value m.product_description }, "Select a processor")
   }
}

但只显示尽可能多的下拉列表,因为我得到的搜索句子产品,它显示在每一个名单只有一个结果。

But it shows only as many Drop Down List as I get products from the search sentence and it show only one result in every list.

我应该怎么办?或者我应该怎样建立这种类型的应用程序?也许Web窗体应该更容易做到这一点简单的应用程序,但我需要尝试使用极限编程,方法包括测试驱动开发,我明白,这是不可能的Web窗体。那么,这个XP是另一个故事...

What should I do? Or how should I build this kind of application? Perhaps Web Forms should be easier to do this simple application but I need to try to use the methods of eXtreme Programming, including Test Driven Development and I understood that that isn't possible with Web Forms. Well, this XP is another story...

非常感谢。

推荐答案

假设你的操作是这样的:

Assuming your action looks like this:

public ActionResult Index()
{
    IEnumerable<Product> products = Repository.ListAll();
    return View(products);
}

和对应的视图是强类型为的IEnumerable&LT;产品与GT;

And the corresponding view is strongly typed to IEnumerable<Product>:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Collections.Generic.IEnumerable<YourNamespace.Product>>" %>

您不需要使用的foreach 语句生成下拉框:

you don't need to use a foreach statement to generate the dropdown box:

<%= Html.DropDownList("processor", Model.Select(p => new SelectListItem { 
    Text = p.product_description, 
    Value = p.product_id 
})) %>

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

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