Asp.net mvc 3 在asp.net MVC3中将数据库中的数据显示到DropDownList中

Asp.net mvc 3 在asp.net MVC3中将数据库中的数据显示到DropDownList中,asp.net-mvc-3,sql-server-2008,html-select,Asp.net Mvc 3,Sql Server 2008,Html Select,我正试图从数据库中获取字段数据并显示在下拉列表中。我没有得到正确的输出。我在下拉列表中获得模型类名 模型名称:SearchMDLNoModel using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace ApricaCRMEvent.Models.CRM.DatabaseEnt

我正试图从数据库中获取字段数据并显示在下拉列表中。我没有得到正确的输出。我在下拉列表中获得模型类名

模型名称:SearchMDLNoModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ApricaCRMEvent.Models.CRM.DatabaseEntities
{
    public class SearchMDLNoModel
    {
        [Display(Name = "Request For Id")]
        public string Request_For_Id { get; set; }
    }
}
数据层类名称:SearchMDLNoDL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ApricaCRMEvent.Models.CRM.DatabaseEntities;
using DataLayer;
using System.Data;

namespace ApricaCRMEvent.Models.CRM.DataLayer
{
    public class SearchMDLNoDL
    {
        public static List<SearchMDLNoModel> getAllMDLno() //all details of SQue
        {
            string proc = "SPGetMDLno";
            DataTable table = DataProvider.SelectStoreProcedure(proc);
            List<SearchMDLNoModel> ListMDLno = new List<SearchMDLNoModel>();

            foreach (DataRow row in table.Rows)
            {
                SearchMDLNoModel MDLno_Obj = new SearchMDLNoModel();
                MDLno_Obj.Request_For_Id = Convert.ToString(row["Request_For_Id"]);

                ListMDLno.Add(MDLno_Obj);
            }
            return ListMDLno;
        }

    }
}
查看名称:Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ApricaCRMEvent.Models.CRM.DatabaseEntities.SearchMDLNoModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Search by MDLNo</h2>
  <% using (Html.BeginForm()) { %>
  <%: Html.ValidationSummary(true, "Profile Updation was unsuccessful. Please correct the errors and try again.") %>
  Select MDLno 
  <%= Html.DropDownList("Request_For_Id", ViewData["MDLno"] as SelectList)%> 
  <input type="submit" value="search" name="SearchMDLNo" />  
  <% } %>
</asp:Content>

指数
MDLNo搜索
选择MDLno
我得到的输出是这样的。。我想要那个特定字段的数据

您使用的是
选择列表的,其中第二个参数是
selectedValue

您可能需要指定
dataValueField
dataTextField

因此,您应该编写并创建
选择列表
,如下所示:

ViewData["MDLno"] = 
 new SelectList(SearchMDLNoDL.getAllMDLno(), "Request_For_Id", "Request_For_Id");

非常感谢你。我在DropDownletst中获得了正确的数据。:)我知道我需要传递2个参数,但我不知道我应该传递哪一个参数。
ViewData["MDLno"] = 
 new SelectList(SearchMDLNoDL.getAllMDLno(), "Request_For_Id", "Request_For_Id");