Controller code:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using System.Data;
Using System.Configuration;
Using System.Data.SqlClient;
Namespace Display Demo.controllers
{
public class Homecontroller:controller
{
//
GET:/home/
String Strcon = configurationmanager.connectionstrings["Con"]. ConnectionString;
String sql = "SELECT * from Product";
Public ActionResult Index ()
{
using (SqlConnection con = new SqlConnection (Strcon))
{
Con. Open ();
DataSet ds = new DataSet ();
SqlDataAdapter SDA = new SqlDataAdapter (sql, con);
Sda. Fill (DS);
viewdata["Getds"] = ds;
}
return View ();
}
}
}
View Code
<%@ page language= "C #" inherits= "system.web.mvc.viewpage<dynamic>"%>
<! DOCTYPE html>
<meta name= "viewport" content= "Width=device-width"/>
<title>Index</title>
<style type= "Text/css" >
Table {
BORDER:2PX double red;
}
th {
width:100px;
}
</style>
<body>
<div>
<%system.data.dataset Setds = (System.Data.DataSet) viewdata["Getds"];%>
<table id= "DT" >
<tr>
<th> Product id</th>
<th> Product Name </th>
<th> Product Description </th>
<th> Product url</th>
<th> Price </th>
<th>ID</th>
</tr>
<%system.data.datatable dt = Setds. Tables[0]; %>
<%foreach (System.Data.DataRow Dr in Dt. Rows)
{%>
<tr>
<td><%:d r[0]%></td>
<td><%:d R[1]%></td>
<td><%:d r[2]%></td>
<td><%:d R[3]%></td>
<td><%:d R[4]%></td>
<td><%:d r[5]%></td>
</tr>
<%}%>
</table>
</div>
</body>
This article is from the "Day to Up" blog, so be sure to keep this source http://1433189426.blog.51cto.com/8055494/1570313
Binding database files to the foreground page through the controller in MVC