0
其实我使用MVC和要显示从数据库中,我创建了一个模型类为包含代码如何在MVC中显示数据记录到表中?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MvcApplication3.Models
{
public class DBConnection
{
static string con = ConfigurationManager.ConnectionStrings["ABC"].ToString();
SqlConnection sn = new SqlConnection(con);
SqlCommand sm;
public DataSet fillGridDAL(string str, string param, string value)
{
try
{
sm = new SqlCommand(str, sn); //("select St_Id_int,St_StateName_var from tbl_State_Master", sn);
sm.CommandType = CommandType.StoredProcedure;
//if (param != null)
sm.Parameters.Add(param, value);
DataSet ds = new DataSet();
SqlDataAdapter sd = new SqlDataAdapter(sm);
sd.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw;
}
}
}
}
和一个控制器中加入以下代码
dbconnection.cs记录到table.for第一次using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication3.Models;
using System.Data;
using System.Data.SqlClient;
namespace MvcApplication3.Controllers
{
public class StudentController : Controller
{
//
// GET: /Student/
public ActionResult Index()
{
DBConnection db = new DBConnection();
DataSet ds = db.fillGridDAL("Student_View","@Stud","1");
ViewBag.StudentList = ds;
return View(ds);
}
}
}
现在我想表明一种观点认为加
@model MvcApplication3.Models.DBConnection
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table>
<tr>
<td style="background-color: #800080; color: #FFFFFF; font-family: 'Times New Roman', Times, serif;
font-size: large; border-style: inset; border-width: thin">
ID:
</td>
<td style="background-color: #800080; color: #FFFFFF; font-family: 'Times New Roman', Times, serif;
font-size: large; border-style: inset; border-width: thin">
Sname:
</td>
<td style="background-color: #800080; color: #FFFFFF; font-family: 'Times New Roman', Times, serif;
font-size: large; border-style: inset; border-width: thin">
City:
</td>
<td style="background-color: #800080; color: #FFFFFF; font-family: 'Times New Roman', Times, serif;
font-size: large; border-style: inset; border-width: thin">
Address:
</td>
<td style="background-color: #800080; color: #FFFFFF; font-family: 'Times New Roman', Times, serif;
font-size: large; border-style: inset; border-width: thin">
Marks:
</td>
</tr>
</table>
任何人都可以在HEL DS table.for结果我要在表格中显示数据集结果......?
感谢Harikant ....这作品..... – user1838502
你好Harikant,我的下一个问题是我怎么可以添加按钮点击事件像经典的ASP.for插入更新删除 – user1838502
我们在MVC中没有事件处理程序。你使用AJAX来添加更新删除 – Harikant