Display data From database in table format and List Formats in MVC C#

Models folder
...............................................

Employee.cs
..............
  public class Employee
    {
        public int EmpNo { get; set; }
        public string EmpFirstName { get; set; }
        public string EmpLastName { get; set; }
        public int Rank { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public string designation { get; set; }
        public string mobile { get; set; }
    }


Controllers Folder
............................................
ShowController .cs

using InsertAndDisplay.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;

namespace InsertAndDisplay.Controllers
{
    public class ShowController : Controller
    {
        //
        // GET: /Show/

        public ActionResult Index()
        {
            SqlConnection connection = new SqlConnection("data source=192.168.200.77;user id=sa;password=indi123;database=pluto");
            SqlDataAdapter da = new SqlDataAdapter("select name, designation, mobile, id, Rank from Employee", connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<Employee> objlstEmployee = new List<Employee>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Employee objEmployee = new Employee();
                objEmployee.name = dt.Rows[i]["name"].ToString();
                objEmployee.designation = dt.Rows[i]["designation"].ToString();
                objEmployee.mobile = dt.Rows[i]["mobile"].ToString();
                objEmployee.id = dt.Rows[i]["id"].ToString();
                objEmployee.Rank = Convert.ToInt32(dt.Rows[i]["Rank"].ToString());
                objlstEmployee.Add(objEmployee);
            }
            return View(objlstEmployee);
        }
    }

}
View Folder:
.....................................
Display.cshtml

///Display Data in Table Format

@model  IEnumerable<InsertAndDisplay.Models.Employee>
@{
    ViewBag.Title = "Index";
}
<table border="1px">
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @item.name
            </td>
            <td>
                @item.id
            </td>
            <td>
                @item.mobile
            </td>
            <td>
                @item.Rank
            </td>
            <td>
                @item.designation
            </td>

        </tr>
    }
</table>

//Displa Data in Un Order List Format

<ul>

    @foreach (var item in Model)
    {
        <li>

            @item.name
        </li>
        <li>
            @item.id
        </li>
        <li>
            @item.mobile
        </li>
        <li>
            @item.Rank
        </li>
        <li>
            @item.designation
        </li>

    }
</ul>

//Display data in Order list Format

<ol>

    @foreach (var item in Model)
    {
    <li>

        @item.name
    </li>
        <li>
            @item.id
        </li>
        <li>
            @item.mobile
        </li>
        <li>
            @item.Rank
        </li>
        <li>
            @item.designation
        </li>

    }
</ol>

Comments

Popular posts from this blog

How to write Pure java script Program?