Thursday, 6 January 2022

Display Data In Table/Tabular Format In Asp Net Core 6/MVC Using C#.Net (Ms Sql Database)

1/06/2022 - By Pranav Singh 0

 In this article i will show you how you can display data in table r tabular format in asp.net core 6 or mvc using c#.net using ms sql database.  Here are the links of my previous articles Asp.Net Core 6: Connect To Sql Server Database With Entity Framework Using C#Access Connection String In Asp.Net Core 6 Controller From appsettings.json C#appsettings.json Connection String Value Access in Asp.net Core ControllerHow To Change Default Page Or View In Asp.Net Core 6


First we will a sql table and add some data on it. Here i have created an employee table and added some record.


employee sql table


So for this article we will create a new asp.net core mvc application and write the code to access the above employee table and display it on screen. Now will make connection with data base using entity framework  in c#.net. 


In this article i will only explain how to get data from data base and display the data on screen in your asp.net core 6/mvc application. To know how to make connection with sql database "Asp.Net Core 6: Connect To Sql Server Database With Entity Framework Using C#". 


Ones you done with connection just check your db context file for connection string.


   protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

        {

            if (!optionsBuilder.IsConfigured)

            {

#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.

                optionsBuilder.UseSqlServer("Server=..\\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;");

            }

        }

 

DBContext in asp.net core 6/mvc

Now for this first we will crate a model class file and add the below code into it class file. Here the class file name i have taken is Employee.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace Project.Models

{

    public class EmployeeModel

    {

        public List<EmplDetail> EmplDetailList { get; set; }

    }

    public class EmplDetail

    {

        public int Id { get; set; }

        public string EmployeeName { get; set; }

        public string Designation { get; set; }

        public string Department { get; set; }

        public int? Salery { get; set; }

    }

}

 

Now we will create a new HttpGet method in controller. 


  [HttpGet]

        public IActionResult Index()

        {

            return View();

        }


 Now we will create view and add the below code into the view (.cshtml) file. 

 

@model Project.Models.EmployeeModel

@{

    ViewData["Title"] = "Home Page";

}

<table border="1">

    <thead>

        <tr>

            <th>ID</th>

            <th>EMPLOYEE NAME</th>

            <th>DEPARMENT</th>

            <th>DESIGNATION</th>

            <th>SALERY</th>

        </tr>

    </thead>

    <tbody>

        @foreach (var item in Model.EmplDetailList)

        {

            <tr>

                <td>@item.Id</td>

                <td>@item.EmployeeName</td>

                <td>@item.Department</td>

                <td>@item.Designation</td>

                <td>@item.Salery</td>

            </tr>

        }

    </tbody>

</table>


In above code i have included Employee model class and applied loop for binding the html table.  Now we have done run the code to check the output. Please check the code break point. 

Display Data In Table/Tabular Format In Asp Net Core 6/MVC Using C#.Net (Ms Sql Database)


Now press F5 and check the output.


Display Data In Table/Tabular Format In Asp Net Core 6/MVC Using C#.Net (Ms Sql Database)
Downloa

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top