如何在MVC中将记录表单数据集显示到表中?

时间:2014-04-22 05:46:44

标签: asp.net-mvc

实际上我是第一次使用MVC并希望将数据库中的记录显示到table.for我创建了一个模型类作为包含代码的dbconnection.cs

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

并添加一个控制器以及以下代码

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);
        }

    }
}

现在我想在table.for中显示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>

任何人都可以帮我在表格中显示数据集结果......?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

@using System.Data;

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

&#13;
&#13;
<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>
&#13;
&#13;
&#13;