将数据从数据库显示到asp.net MVC3中的DropDownList

时间:2013-01-30 09:24:09

标签: 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.DatabaseEntities
{
    public class SearchMDLNoModel
    {
        [Display(Name = "Request For Id")]
        public string Request_For_Id { get; set; }
    }
}

DataLayer类名称: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;
        }

    }
}

控制器名称:SearchMDLNoController

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

namespace ApricaCRMEvent.Controllers
{
    public class SearchMDLNoController : Controller
    {
        //
        // GET: /SearchMDLNo/

        public ActionResult Index()
        {

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

            return View();

        }

    }
}

查看名称: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>

我得到这样的输出..我想要那个特定领域的数据。

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在使用SelectList的{​​{3}},其中第二个参数是。{1}} selectedValue

您可能需要wrong constructor,您可以在其中指定dataValueFielddataTextField

所以你应该写这样的SelectList

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