TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
Reply
Answers (
1
)
How to Get and insert IP Address in Asp.net C#
405 error pls help in dot net