All Master in one place.

0
68

Introduction

In a project, we need master like Gender, Marital Status, Employee Type and many more. We always use Enum or hard code value in Select tag of dropdown for master. As our project progress, we need more master and in such case, it is very difficult to manage.  

Background

Project like ERP has master like Tax Type, Customer Type, Invoice Type, Discount Type, Price Type, Shipment Type etc. So, our target is to bring all Master in one place.

Using the code

First Create 2 table i.e. GeneralMaster and GeneralMasterDetails

CREATE TABLE [dbo].[GeneralMaster](     [ID] [int] IDENTITY(1,1) NOT NULL,     [Name] [varchar](50) NULL,     [Description] [varchar](50) NULL,  CONSTRAINT [PK_GeneralMaster] PRIMARY KEY CLUSTERED  (     [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[GeneralMasterDetails](     [GMDetailsID] [int] IDENTITY(1,1) NOT NULL,     [GMID] [int] NOT NULL,     [Name] [varchar](50) NULL,     [Description] [varchar](50) NULL,  CONSTRAINT [PK_GeneralMasterDetails] PRIMARY KEY CLUSTERED  (     [GMDetailsID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]

After data enter it will look like following ……

Now, how to Use General Master in DropDown ……

Create new project in VS, then add new item select ADO.Net Entity Data Model, Name Entity as GMModel.cs

Select Code first

After this, it will ask for database name and connection string.

Finally select table and click finish …..

Now we have 3 class i.e. GeneralMaster.cs, GeneralMasterDetail.cs and GMModel.cs

Create new class in model, name it as GeneralMasterModel, and add following code …

namespace GeneralMasterProj.Models {     public class GeneralMasterModel     {         public GeneralMasterModel()         {             GenderList = new List();             MaritalStatusList = new List();             SalutationList = new List();             TitleList = new List();             EmployeeTypeList = new List();             MonthList = new List();         }                         public int GMDetailsID { get; set; }         public IList GenderList { get; set; }         public IList MaritalStatusList { get; set; }         public IList SalutationList { get; set; }         public IList TitleList { get; set; }         public IList EmployeeTypeList { get; set; }         public IList MonthList { get; set; }       } }

Now in Home Controller add following code

private GMModel db = new GMModel();                  public ActionResult Index()         {             var model = new GeneralMasterModel();             model.GenderList.Add(new SelectListItem {                 Text = "Select Gender",                 Value = "0"             });             foreach(var masterdetails in GeneralMaster(1))             {                 model.GenderList.Add(new SelectListItem {                     Text = masterdetails.Name,                     Value = masterdetails.GMDetailsID.ToString()                 });             }             model.MaritalStatusList.Add(new SelectListItem             {                 Text = "Select Marital Status",                 Value = "0"             });             foreach (var masterdetails in GeneralMaster(2))             {                 model.MaritalStatusList.Add(new SelectListItem                 {                     Text = masterdetails.Name,                     Value = masterdetails.GMDetailsID.ToString()                 });             }             model.SalutationList.Add(new SelectListItem             {                 Text = "Select Salutation",                 Value = "0"             });             foreach (var masterdetails in GeneralMaster(3))             {                 model.SalutationList.Add(new SelectListItem                 {                     Text = masterdetails.Name,                     Value = masterdetails.GMDetailsID.ToString()                 });             }             model.TitleList.Add(new SelectListItem             {                 Text = "Select Title",                 Value = "0"             });             foreach (var masterdetails in GeneralMaster(4))             {                 model.TitleList.Add(new SelectListItem                 {                     Text = masterdetails.Name,                     Value = masterdetails.GMDetailsID.ToString()                 });             }             model.EmployeeTypeList.Add(new SelectListItem             {                 Text = "Select Employee Type",                 Value = "0"             });             foreach (var masterdetails in GeneralMaster(5))             {                 model.EmployeeTypeList.Add(new SelectListItem                 {                     Text = masterdetails.Name,                     Value = masterdetails.GMDetailsID.ToString()                 });             }             model.MonthList.Add(new SelectListItem             {                 Text = "Select Month",                 Value = "0"             });             foreach (var masterdetails in GeneralMaster(6))             {                 model.MonthList.Add(new SelectListItem                 {                     Text = masterdetails.Name,                     Value = masterdetails.GMDetailsID.ToString()                 });             }             return View(model);         }         public IEnumerable GeneralMaster(int id)         {             return db.GeneralMasterDetails.ToList().Where(e => e.GMID == id);         }

The Method GeneralMaster(int id) return list  … if we provide 1 or 2 or 5 Output will be as following

Now in Index.cshtml file add following code ….

@model GeneralMasterProj.Models.GeneralMasterModel @{     ViewBag.Title = "Home Page"; } <div class="row">     <div class="col-lg-12">         <h1 class="page-header">All General Master     
    <!-- /.col-lg-12 -->
@*@using (Html.BeginForm())*@ @using (Html.BeginForm("CreateNewUser", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {     @Html.AntiForgeryToken()     <div class="row">                   <div class="col-sm-9">                                  <div class="form-group col-sm-4">                     <label class="formlabel">Gender                         @Html.DropDownListFor(model => model.GMDetailsID, Model.GenderList, htmlAttributes: new { @class = "form-control", @required = "" })