如何填充下拉菜单?

时间:2011-09-27 08:30:00

标签: c# asp.net html asp.net-mvc

我正在尝试填充下拉菜单。到目前为止我已经:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<HTML>
<HEAD>
<TITLE>NZ Currency Converter</TITLE>
 </HEAD>
<BODY>
<p><tr>
<td colspan="2" align="right">&nbsp;&nbsp;The other currency is:</td>
<td>
  <select name="the other currency is:">
    <% List<Currency.Models.exchrate> exchrateList = (List <Currency.Models.exchrate>) ViewData["exchrateList"];
       foreach (Currency.Models.exchrate st in exchrateList)
       {
    %>  
       <option value="<% Response.Write(st.othercurrency);%>">"<% Response.Write(st.fromnzd);%>">"
<% Response.Write (st.tonzd);%>">
</option> 
    <% } %>
    <option value=""></option>
  </select>
     </td>
</tr>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Currency.Models;

namespace Currency.Controllers
{
[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        financeInit();
        return View();
    }

    public void financeInit()
    {

        financeEntities db = new financeEntities();
        ViewData["exchrate"] = db.exchrates.ToList();
        ViewData["Convert From NZD"] = "";
        ViewData["Convert To NZD"] = "";
        ViewData["You wish to convert"] = "";
        ViewData["That will produce"] = "";


    }
    public ActionResult financeCurrencyConvert()
    {
        return View();
    }
         }
    }

这是我的家庭控制器视图,该视图链接到将从中填充下拉菜单的数据库。我还能添加什么呢?我是asp.net的新手,不知道Web开发是如何工作的,所以任何帮助都将受到赞赏。在下拉菜单中,而不是国家我获得他们的转换率。任何人都可以帮助如何更改此选择作为国家

1 个答案:

答案 0 :(得分:0)

问题是您在控制器和视图中使用不同的视图数据键(控制器中的"The Other Currency is:"和视图中的"exchrateList")。匹配它们你应该没问题 - 例如,从控制器

更改此行
ViewData["The Other Currency is:"] = db.exchrates.ToList();

ViewData["exchrateList"] = db.exchrates.ToList();