0

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

型号名称: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;
        }

    }
}

控制器名称: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>

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

在此处输入图像描述

4

1 回答 1

1

您使用了错误的构造函数SelectList其中第二个参数是 selectedValue.

您可能需要此构造函数,您可以在其中指定dataValueFielddataTextField

所以你应该这样写SelectList

ViewData["MDLno"] = 
 new SelectList(SearchMDLNoDL.getAllMDLno(), "Request_For_Id", "Request_For_Id");
于 2013-01-30T09:29:04.023 回答