2010-02-24 99 views
2

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

我不熟悉MVC模型,所以我不太清楚应该在哪个文件夹中放置应用程序的哪一部分。现在我有

-Controllers:

--HomeController

-Models

--HomeRepository

--IHomeRepository

--database。 dbml(现在我只使用一个名为prod的表UCT和我需要有

PRODUCT_DESCRIPTION和PRODUCT_PRICE)

-views

- 家居

----指数

的信息---- ...等...

我已经设法从产品表中的所有产品列入项目符号列表a nd在索引页面显示它。所以,我的HomeRepository从Database.dbml中创建一个数据上下文。还有一个公共的IList ListAll()方法(?),其中写入了搜索语句。 IHomeRepository只有

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

不知何故,它有效,一段时间我很高兴。我试图填充一个下拉列表中的索引页是这样的:

<% 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") 
    } 
} 

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

我该怎么办?或者我应该如何构建这种应用程序?也许Web窗体应该更容易做到这一简单的应用程序,但我需要尝试使用极限编程的方法,包括测试驱动开发,并且我明白这对于Web窗体来说是不可能的。那么,这个XP是另一回事......

非常感谢。

+0

关于此问题的好文章:http://www.277hz.co.uk/Blog/Show/7/image-uploading-in-asp-net-mvc – 2011-06-02 19:03:58

回答

4

假设你的操作是这样的:

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

以及相应的视图是强类型到IEnumerable<Product>

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

你不需要使用foreach语句来产生下拉框中:

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

感谢您的回答,Darin。我尝试过,但它没有得到它的工作。但尝试时,我发现了另一个有效的例子。现在我的指标是这样的:

[...] 
    <% using (Html.BeginForm()) { %> 
     <table> 
      <tr> 
      <td>Processor</td> 
      <td><%= Html.DropDownList("lstProcessor1", new SelectList((IEnumerable)ViewData["Processor1List"], "product_price", "product_description")) %></td> 
      </tr> 
      <tr> 
      <td>Total Amount</td> 
      <td>0,00 €</td> 
      </tr> 
     </table> 
     <input type="submit" value="Submit" /> 
    <% } %> 
    [...] 

而且HomeController中的开头:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Mvc.Ajax; 
using MvcApplication1.Models; 

namespace MvcApplication1.Controllers 
{ 
    [HandleError] 
    public class HomeController : Controller 
    { 
     // Connect database 
     DB50DataContext _ctx = new DB50DataContext(); 

     // GET: /Home/ 
     public ActionResult Index() 
     { 
      // Search: Processors 
      var products = from prod in _ctx.products 
          where prod.product_searchcode == "processor1" 
          select prod; 

      ViewData["Processort1List"] = products; 

      return View(); 
     } 

所以我不使用存储库现在,用那种解决方案,我知道我可以写里面的公共查询ActionResult Index()获取其他组件,但我认为这不是它应该的方式?这些数据库查询不应该在别的地方吗?该应用程序将只有家庭文件夹,所有页面都在里面。

我还有下拉列表的另一个问题:是否有可能在Text属性中显示product_description和product_price?我尝试了“product_description + product_price”类的东西,但当然不起作用。