ashim kc

ashim kc

  • NA
  • 11
  • 962

dropdownlist in mvc from businesslayer

Jul 12 2018 6:55 AM
Hi
I am new in MVC. I am trying to create a dropdown list for company code and name , to edit the Company Master.
In BusinessLayer namespace I have two class file
1. Company.cs
2. CompanyBL.cs
In Company.cs
namespace BusinessLayer
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
public class Company
{
[Required]
public string Code { get; set; }
[Required]
public string CompanyName { get; set; }
[Required]
}
}
In CompanyBL.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLayer
{
public class CompanyBL
{
public IEnumerable<Company> ListCompany
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString;
List<Company> ListCompany = new List<Company>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spGetAllCompany", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Company _company = new Company();
_company.Code = rdr["Code"].ToString();
_company.CompanyName = rdr["CompanyName"].ToString();
ListCompany.Add(_company);
} // for loop
} // Sql Connection
return ListCompany;
} //Get
}
In myproject namespace
In HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer;
namespace myProject.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult EditCompany()
{
CompanyBL _companybl = new CompanyBL();
return View();
}
}
}
Here, I would like to create a view file with name EditCompany.cshtml and in which I would like to create the drop downlist for the company code. Dataname should be the Code and display name should be the Company Name.
Please can you go through my code and please anybody can explain me in simple steps how to create the dropdownlistfor company code
Many Thanks

Answers (1)