2014-04-22 46 views
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結果我要在表格中顯示數據集結果......?

回答

0

@using System.Data;

@ {

 DataTable dtable = ViewBag.StudentList[0]; 

} 


<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> 

@foreach (DataRow dr in dtable.Rows) 
    { 
    <tr>     
       <td style="background-color: #800080; color: #FFFFFF; font-family: 'Times New Roman', Times, serif; 
        font-size: large; border-style: inset; border-width: thin"> 
        @dr["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"> 
        @dr["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"> 
        @dr["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"> 
        @dr["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"> 
        @dr["Marks"] 
       </td> 
      </tr> 

} 

    </table> 
+0

感謝Harikant ....這作品..... – user1838502

+0

你好Harikant,我的下一個問題是我怎麼可以添加按鈕點擊事件像經典的ASP.for插入更新刪除 – user1838502

+0

我們在MVC中沒有事件處理程序。你使用AJAX來添加更新刪除 – Harikant

0
@using System.Data; 

@{ 
    DataSet ds = ViewBag.StudentList; 
    DataTable dt = ds.Tables[0]; 
} 

<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> 
 

 
@foreach (DataRow dr in dtable.Rows) 
 
    { 
 
    <tr>     
 
       <td style="background-color: #800080; color: #FFFFFF; font-family: 'Times New Roman', Times, serif; 
 
        font-size: large; border-style: inset; border-width: thin"> 
 
        @dr["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"> 
 
        @dr["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"> 
 
        @dr["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"> 
 
        @dr["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"> 
 
        @dr["Marks"] 
 
       </td> 
 
      </tr> 
 

 
} 
 

 
    </table>

相關問題