This
article will show you how you can create a web grid data bind in asp.net mvc
using c# with image bind and shorting feature of web grid on header click.
So for this article first we will create a new asp.net application and add a model class file. After this model class file we will add the below model class code.
So for this article first we will create a new asp.net application and add a model class file. After this model class file we will add the below model class code.
public class CountryModel
{
public List<Country> CountryList {
get; set; }
}
public class Country
{
public string CountryName { get; set; }
public int Population { get; set; }
public string FlagURL { get; set; }
}
|
This code we used for providing the model class collection of data. Now we will create the model controller class file. Ad add the below code.
[HttpGet]
public ActionResult
Index()
{
CountryModel
_countryModel = new CountryModel();
_countryModel.CountryList= GetCountryList();
return
View(_countryModel);
}
///
/// Country list
///
public List<Country> GetCountryList()
{
List<Country> _country = new List<Country>();
_country.Add(new Country { CountryName = "India", Population = 124000, FlagURL = "CountryFlag/india.png" });
_country.Add(new Country { CountryName = "China", Population = 84545, FlagURL = "CountryFlag/china.png" });
_country.Add(new Country { CountryName = "Germany", Population = 9354999, FlagURL = "CountryFlag/germany.png" });
_country.Add(new Country { CountryName = "Japan", Population = 24999, FlagURL = "CountryFlag/japan.png" });
return _country;
}
|
Hear in above
code I have created a function which will provide data to the model and and
return to the view for displaying it. Now we will create a view and add the
below code.
@model MVC_Demos.Models.CountryModel
@{
ViewBag.Title
= "Asp.Net MVC : WebGrid Image
Binding With Shorting Data Using C#.Net";
}
<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
@using (Html.BeginForm("Index", "Home"))
{
<table width="100%" cellpadding="5" cellspacing="2" border="0" style="background-color: White;">
<tr>
<td>
@{
var grid = new WebGrid(source:
Model.CountryList,
rowsPerPage:
10,
canSort: true);
}
@grid.GetHtml(
tableStyle: "gridtable",
alternatingRowStyle: "even",
columns: grid.Columns(
grid.Column("CountryName", "CountryName"),
grid.Column("Population",
"Population"),
grid.Column("FlagURL", header: "Country
Flag", format: @
)
))
</td>
</tr>
</table>
}
|
In above
code please check the highlighted part of the code. This piece of code is responsible
for shorting the grid data by clicking on header of the grid. Now we have done
run the application to check the output.
0 comments:
Please let me know your view