从数据库显示的数据为DropDownList的asp.net中MVC3 [英] Display data from the database into DropDownList in asp.net MVC3

查看:111
本文介绍了从数据库显示的数据为DropDownList的asp.net中MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从下拉列表中选择数据库并显示获取现场数据。我没有得到正确的输出。我在下拉菜单中得到的模型类名。

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.

型号名称:SearchMDLNoModel

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; }
    }
}

数据层类名称:SearchMDLNoDL

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;
        }

    }
}

控制器名称:SearchMDLNoController

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();

        }

    }
}

查看名称:Index.aspx的

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.

推荐答案

您正在使用的的SelectList ,其中第二个参数是错误的构造
了selectedValue

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

您可能需要此构造在这里您可以指定 dataValueField dataTextField

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

所以,你应该写创建的SelectList 是这样的:

So you should write create your SelectList like this:

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

这篇关于从数据库显示的数据为DropDownList的asp.net中MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆