How to display database values in table in mvc using entity framework

As previous we have seen how to bind webgrid in mvc using ado operation. But now we have seen how to bind or show database values in the table using entity framework.
Database:-




 

Creating an Entity Data Model:-

Step1: - After creating an application we click on the right click on model and choose ADO.NET Entity Data Model


 




Step2: -Then the Entity Data Model Wizard will open up where we select EF Designer database option.




Step4-We make a database connection (connection string) and then click Test Connection to make sure all settings are correct.








Step5: - The Connection String is generated




Step6: We get all tables from database and we select our table record





Step7: -Now we have the Entity Data Model ready with the record Table





Controller:-
using MVCCODESOLUTIONS.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;

using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace MVCCODESOLUTIONS.Controllers
{
    public class HomeController : Controller
    {
      
        public ActionResult Index()
        {

            CodeSolutionEntities entities = new CodeSolutionEntities();
            return View(from record in entities.records
                        select record);
        }

    }
}




View:-
@model IEnumerable<MVCCODESOLUTIONS.Models.record>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>

<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }

        th, td {
            text-align: left;
            padding: 8px;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        th {
            background-color: #4CAF50;
            color: white;
        }
    </style>

</head>
<body>
    <h1>How to display database values in table in mvc using entity framework </h1>
    <hr />
    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
            <th>Email</th>

        </tr>
        @foreach (MVCCODESOLUTIONS.Models.record data in Model)
        {
            <tr>
                <td>@data.record_id</td>
                <td>@data.Name</td>
                <td>@data.Age</td>
                <td>@data.Email</td>

            </tr>
        }
    </table>

</body>
</html>
Out-Put:-




How to display database values in table in mvc using entity framework How to display database values in table in mvc using entity framework  Reviewed by NEERAJ SRIVASTAVA on 10:43:00 AM Rating: 5

1 comment:

  1. Different type of platform required to opinion from their visitor but I am excited to read about a new feature of essay writing. It is good for all users who have an account on this platform, they easily write on child support topics and multiple topics using their mind but this custom essay writing service is the best source for complete work with the help of our experienced qualified writers.

    ReplyDelete

Powered by Blogger.