Sunday 24 September 2017

Pass (Send) DataSet (DataTable) from Controller to View in ASP.Net MVC

Pass (Send) DataSet (DataTable) from Controller to View in ASP.Net MVC

Description:


In this example we explain that how to Send DataSet or DataTable from Controller to Razor View in asp.Net MVC.or how to pass DataSet or DataTable from controller to view in MVC 4.or or to retrieve or read dataset or DataTable data from controller to view in asp.net MVC.or how to pass DataSet or Datatable data from controller to view in MVC.

Here we demonstrate that how to call controllers method and pass or send its DataTable or DataSet from controller to vie in Asp.Net MVC.
Controller:

public class EmployeeController : Controller
    {

        public ActionResult Index()
        {
            DataSet ds = new DataSet();
            string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                string query = "SELECT * FROM Employees";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        sda.Fill(ds);
                    }
                }
            }

            return View(ds);
        }
    }

View:

@using System.Data
@model DataSet
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Pass (Send) DataSet (DataTable) from Controller to View in ASP.Net MVC</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>EmployeeID</th>
            <th>ContactName</th>
            <th>Country</th>
        </tr>
        @foreach (DataRow row in Model.Tables[0].Rows)
        {
            <tr>
                <td>@row["EmployeeId"]</td>
                <td>@row["Name"]</td>
                <td>@row["Country"]</td>
            </tr>
        }
    </table>
</body>
</html>



This entry was posted in :

0 comments:

Post a Comment