如何填充下拉菜单

本文关键字:下拉菜单 填充 何填充 | 更新日期: 2023-09-27 18:09:39

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

<%@ 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开发工作,所以任何帮助将不胜感激。在下拉菜单,而不是国家,我得到他们的转化率。谁能帮助如何改变这个选择作为国家

如何填充下拉菜单

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

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

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