let take an example
How to fetch data from database to dropdownlist for
First Create a table called Gender Table
Sql Query:-
CREATE TABLE [dbo].[Gender]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] VARCHAR(50) NULL,
[Status] BIT NULL
)
execute it....
table will create...
After that insert the Value inside the table as per given in Image.
Now
-> Open new Project and Name TestProject in Mvc in your Visual Studio.
After creation of Test Project .
-> Go to Model Folder Create a Class of Name Home page which contain GenderId field. as ViewModel not map with Table.
>And Create another Model GenderModel.cs and Map with Table using DataAnnotation.Schema
as [Table("Gender")] .
>Create a class of DefaultConnection.cs accoding given name of Connection Web.config file.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-ItsMe-20160710025752.mdf;Initial Catalog=aspnet-ItsMe-20160710025752;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
> Inherit DefaultConnection.cs from derived class DbContext as a Base class.
and map with the model GenderModel using DbSet.
What is DbSet?
:- It is a class represent to entity set which we can do read, write ,update and delete operation.
:- Generic version is DbSet<TEntity>.
>Add DefaultConnection which we can interact with table with the help of DbContext.
-Created the Dropdownlist method.
- we call connection.gendermodelContext. to access the Value f Gender details from Gender table of database.
- connection is the object of DefaultConnection.cs Class.
public void Dropdownlist(HomePage model)
{
var gender = connection.gendermodelContext.Where(r => r.Status == true).ToList();
model.AvailbleGender.Add(new SelectListItem { Text = "Select.....", Value = "0" });
foreach (var i in gender)
model.AvailbleGender.Add(new SelectListItem { Text = i.Name, Value = i.ToString() });
}
- Dropdownlist(model) call in Index Action so that in View page Gender dropdown will List with Value.
> I call the dropdowncontroler like this way.
@Html.DropDownListFor(r=>r.GenderId, Model.AvailbleGender, new { @class = "form-control" })
How to fetch data from database to dropdownlist for
First Create a table called Gender Table
Sql Query:-
CREATE TABLE [dbo].[Gender]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] VARCHAR(50) NULL,
[Status] BIT NULL
)
execute it....
table will create...
After that insert the Value inside the table as per given in Image.
Now
-> Open new Project and Name TestProject in Mvc in your Visual Studio.
After creation of Test Project .
-> Go to Model Folder Create a Class of Name Home page which contain GenderId field. as ViewModel not map with Table.
>And Create another Model GenderModel.cs and Map with Table using DataAnnotation.Schema
as [Table("Gender")] .
>Create a class of DefaultConnection.cs accoding given name of Connection Web.config file.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-ItsMe-20160710025752.mdf;Initial Catalog=aspnet-ItsMe-20160710025752;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
> Inherit DefaultConnection.cs from derived class DbContext as a Base class.
and map with the model GenderModel using DbSet.
What is DbSet?
:- It is a class represent to entity set which we can do read, write ,update and delete operation.
:- Generic version is DbSet<TEntity>.
>Add DefaultConnection which we can interact with table with the help of DbContext.
-Created the Dropdownlist method.
- we call connection.gendermodelContext. to access the Value f Gender details from Gender table of database.
- connection is the object of DefaultConnection.cs Class.
public void Dropdownlist(HomePage model)
{
var gender = connection.gendermodelContext.Where(r => r.Status == true).ToList();
model.AvailbleGender.Add(new SelectListItem { Text = "Select.....", Value = "0" });
foreach (var i in gender)
model.AvailbleGender.Add(new SelectListItem { Text = i.Name, Value = i.ToString() });
}
- Dropdownlist(model) call in Index Action so that in View page Gender dropdown will List with Value.
> I call the dropdowncontroler like this way.
@Html.DropDownListFor(r=>r.GenderId, Model.AvailbleGender, new { @class = "form-control" })
> After executing the project finally data reach to dropdown.....
Thank you..