Display data from the database into DropDownList i

2019-09-08 04:47发布

问题:

I am trying to fetch field data from the database and display in dropdown list. I am not getting proper output. I get model class name in dropdown.

Model name: 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 class name :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;
        }

    }
}

Controller name: 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();

        }

    }
}

View name: 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>

I am getting output like this .. I want data of that particular field.

回答1:

You are using the wrong constructor of the SelectList where the second parameter is the selectedValue.

You probably need this constructor where you can specify the dataValueField and the dataTextField

So you should write create your SelectList like this:

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