Monday, 22 July 2019

Simple way to create dropdwon in MVC with EntityFramework.


Step:1  Create  KeyValuePair Class in ModelLayer

namespace Model
{
    public  class KeyValuePair
    {
        public int Val { get; set; }
        public string label { get; set; }
    }
}



Step:2 Create DropDownModel Model in ModelLayer


using System.Collections.Generic;

namespace Model
{
    public  class DropDownModel
    {
        public int RollNo { get; set; }
        public string Name { get; set; }
       public List<KeyValuePair> KeyValuePair { get; set; }
        public List<KeyValuePair> KeyValuePair1 { get; set; }
        public List<KeyValuePair> KeyValuePair2 { get; set; }
        public List<KeyValuePair> KeyValuePair3 { get; set; }
    }
}


Step:3 Create Class StudentManagement in BussinessLayer.

Step:4 Add Entity in dataLayer.


Step:5 Create Method to get Data from Database using Entity.



using DataLayer;
using Model;
using System.Collections.Generic;
using System.Linq;

namespace BussinessLayer
{
    public class StudentManegament
    {

        public List<KeyValuePair> getDropDownData()
        {
            List<KeyValuePair> keyValuePairs = new List<KeyValuePair>();

            using (var Context = new StudentDBEntities())
            {
                keyValuePairs = (from std in Context.getStudentList() select new KeyValuePair
                {
                    label=std.Name,
                    Val=std.Roll
                }).ToList();
            }

                return keyValuePairs;
        }
    }
}

Step:6 Create Home Controller.

Step:7 Create  Action Method DropDown in Home  Controller.


using BussinessLayer;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DotNetByPriyanshu.Controllers
{
    public class HomeController : Controller
    {
      

        public ActionResult DropDown()
       {
            StudentManegament studentManegament = new StudentManegament();
            DropDownModel dropDownModel = new DropDownModel();
            dropDownModel.KeyValuePair1 = studentManegament.getDropDownData();
            dropDownModel.KeyValuePair2 = studentManegament.getDropDownData();
            dropDownModel.KeyValuePair3 = studentManegament.getDropDownData();

            return View(dropDownModel);
        }

      
    }
}

Step:8 Add View with DropDownModel

@model Model.DropDownModel

@{
    ViewBag.Title = "About";
}

<h2>About</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
<div class="form-horizontal">
    <h4>DropDownModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

 
    <div class="form-group">

        <div class="col-md-5">
            @Html.DropDownListFor(x => Model.RollNo, new SelectList(Model.KeyValuePair1, "Val", "label"), "Select1", htmlAttributes: new { @class = "form-control", @id = "DDlRegion" })
            @Html.ValidationMessageFor(x => x.KeyValuePair, "", new { @class = "text-danger" })
        </div>
    </div>   <div class="form-group">

        <div class="col-md-5">
            @Html.DropDownListFor(x => Model.RollNo, new SelectList(Model.KeyValuePair2, "Val", "label"), "Select2", htmlAttributes: new { @class = "form-control", @id = "DDlRegion" })
            @Html.ValidationMessageFor(x => x.KeyValuePair, "", new { @class = "text-danger" })
        </div>
    </div>   <div class="form-group">

        <div class="col-md-5">
            @Html.DropDownListFor(x => Model.RollNo, new SelectList(Model.KeyValuePair3, "Val", "label"), "Select3", htmlAttributes: new { @class = "form-control", @id = "DDlRegion" })
            @Html.ValidationMessageFor(x => x.KeyValuePair, "", new { @class = "text-danger" })
        </div>
    </div>
  
</div>
}



Database

Table StudentInfo:

/****** Object:  Table [dbo].[StudentInfo]    Script Date: 7/23/2019 2:41:02 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[StudentInfo](
[Roll] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](100) NULL,
[Course] [varchar](100) NULL,
[Mobile] [varchar](100) NULL,
[Address] [varchar](100) NULL,
 CONSTRAINT [PK_StudentInfo] PRIMARY KEY CLUSTERED 
(
[Roll] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO



StoredProcedure [dbo].[getStudentList]:

/****** Object:  StoredProcedure [dbo].[getStudentList]    ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Dot Net By Priyanshu
-- Create date: 12-12-2018
-- Description: getStudentList
-- =============================================
ALTER PROCEDURE [dbo].[getStudentList]
AS
BEGIN
SET NOCOUNT ON;
SELECT [Roll]
      ,[Name]
      
  FROM [StudentDB].[dbo].[StudentInfo]
END

1 comment:

  1. Thanks priyanshu for sharing this blog it's very helpful...

    ReplyDelete