开发者

ASP.NET MVC Drop Down List

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.

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

-Models

--HomeRepository

--IHomeRepository

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

product_description and product_price)

-Views

--Home

---- Index

---- ... etc ...

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.

What should I do? Or how should I build this kind of application? Perhaps Web Forms should be easier to do this simple a开发者_如何学Cpplication 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...

Many thanks.


Assuming your action looks like this:

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

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

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

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


Thanks for you answer, Darin. I tried it but it didn't get it working. But when trying I found another example which worked. Now my Index looks like:

   [...] 
   <% 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" />
   <% } %>
    [...]

And the beginning of 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();
        }

So I'm not using repositories right now and with that kind of solution I understand that I can write queries inside public ActionResult Index() to get other components, but I think this isn't the way it should be? Shouldn't these database queries be somewhere else? The application will only have Home-folder, all pages are inside it.

I have also another problem with Drop Down Lists: Is it possible to show product_description AND product_price in Text property? I tried "product_description + product_price" kind of things but of course that's not working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜