Saturday, August 10, 2019

Part 1: How to display database data in webgrid in mvc 4

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for Get data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >

Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Create a Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Here I have created a controller "UserController"

Step-6: Add new action into your controller for Get Method, which will get data from database


  1. namespace MVCSimpleWebgrid.Controllers
  2. {
  3. public class UserController : Controller
  4. {
  5. //
  6. // GET: /User/
  7. public ActionResult List()
  8. {
  9. var users = new List<UserMaster>();
  10. //here MyDatabaseEntities is the dbcontext
  11. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  12. {
  13. users = dc.UserMasters.ToList();
  14. }
  15. return View(users);
  16. }
  17.  
  18. }
  19. }

Step-7: Add view for your Action & design for show data in webgrid.

[N.B.: Before going to add view for your action, Please rebuild your application]
Right Click on Action Method (here right click on List action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.
HTML Code 
  1. @model List<MVCSimpleWebgrid.UserMaster>
  2. @{
  3. ViewBag.Title = "List of users";
  4. var grid = new WebGrid(source:Model,canPage:true, rowsPerPage:10);
  5. grid.Pager(WebGridPagerModes.All);
  6. }
  7.  
  8. <h2>List of Users</h2>
  9.  
  10. <style type="text/css">
  11. /*Here we will add css for style webgrid*/
  12. .webgrid-table
  13. {
  14. font-family: "Trebuchet MS" , Arial, Helvetica, sans-serif;
  15. font-size: 1.2em;
  16. width: 100%;
  17. display: table;
  18. border-collapse: separate;
  19. border: solid 1px #98BF21;
  20. background-color: white;
  21. }
  22. .webgrid-table td, th
  23. {
  24. border: 1px solid #98BF21;
  25. padding: 3px 7px 2px;
  26. }
  27. .webgrid-header
  28. {
  29. background-color: #A7C942;
  30. color: #FFFFFF;
  31. padding-bottom: 4px;
  32. padding-top: 5px;
  33. text-align: left;
  34. }
  35. .webgrid-footer
  36. {
  37. }
  38. .webgrid-row-style
  39. {
  40. padding: 3px 7px 2px;
  41. }
  42. .webgrid-alternating-row
  43. {
  44. background-color: #EAF2D3;
  45. padding: 3px 7px 2px;
  46. }
  47. </style>
  48.  
  49. <div id="content">
  50. @grid.GetHtml(
  51. tableStyle:"webgrid-table",
  52. headerStyle:"webgrid-header",
  53. footerStyle:"webgrid-footer",
  54. alternatingRowStyle:"webgrid-alternating-row",
  55. rowStyle:"webgrid-row-style",
  56. columns:grid.Columns(
  57. //here i will add column for serial no
  58. grid.Column(header:"Serial No", format:@<text><div>@(item.WebGrid.Rows.IndexOf(item)+1)</div></text>),
  59. grid.Column(columnName:"Username",header:"Username"),
  60. grid.Column(columnName:"FullName", header:"Full Name"),
  61. grid.Column(header:"Email", format:@<text><a href="mailto:@item.EmailID">@item.EmailID</a></text>),
  62. grid.Column(header:"Is Active?", format:@<text><input type="checkbox" checked="@item.isActive" disabled="disabled" /></text>)
  63. ))
  64. </div>
Step-8: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...