Binding database files to the foreground page through the controller in MVC

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.