如何显示asp.net mvc的观点数据库记录 [英] How to display database records in asp.net mvc view

查看:183
本文介绍了如何显示asp.net mvc的观点数据库记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET MVC使用C#,你如何通过一些数据库记录的视图,以表格的形式显示出来?



我需要知道我怎么能从已返回到SqlDataReader对象数据库转移/传递记录一些行和对象传递给视图,以便我可以显示在视图使用的foreach的对象包含的所有记录。



下面的代码是什么,我我想要做的。但它不工作



控制器:

 公众的ActionResult学生()
{
字符串的connectionString =<保证连接字符串HERE>中;
字符串SQL =SELECT * FROM学生;
的SqlCommand CMD =新的SqlCommand(SQL,的connectionString);使用(SqlConnection的的connectionString =新的SqlConnection(的connectionString))
{
connectionString.Open

();
SqlDataReader的RDR = cmd.ExecuteReader();
}

ViewData.Add(生,RDR);

返回查看();
}



查看:

 < H1>学生< / H1> 

<表>
<! - 我怎么在这里显示的记录? - >
< /表>


解决方案

1。首先创建一个模式将保存记录的值。例如:



 公共类学生
{
公共字符串名字{获得;设置;}
公共字符串名字{获得;设置;}
公共字符串类{获取;集;}
....
}

2。然后从你的读者的行装载到一个列表或东西:



 公众的ActionResult学生()
{
字符串的connectionString =<保证连接字符串HERE>中;
字符串SQL =SELECT * FROM学生;
的SqlCommand CMD =新的SqlCommand(SQL,conn);在使用(SqlConnection的康恩=新的SqlConnection(的connectionString))
{
conn.Open()

;
SqlDataReader的RDR = cmd.ExecuteReader();
VAR模型=新的List<的Student GT;();
,而(rdr.Read())
{
变种学生=新学生();
student.FirstName = RDR [名字];
student.LastName = RDR [姓氏];
student.Class = RDR [类];
....

model.Add(学生);
}

}

返回查看(模型);
}



3。最后在你的查看,声明的那种模型的:



  @model名单<的Student GT; 

< H1>学生< / H1>

<表>
< TR>
百分位>首先名称和LT; /第i
<第i个姓< /第i
<第i类< /第i
< / TR>
@foreach(以型号VAR学生)
{
< TR>
< TD> @ student.FirstName< / TD>
< TD> @ student.LastName< / TD>
< TD> @ student.Class< / TD>
< / TR>
}
< /表>


Using ASP.NET MVC with C#, how do you pass some database records to a View and display them in table form?

I need to know how I can transfer/pass some rows of records from a database that have been returned to an SqlDataReader object and pass that object to the View so I can display all the records contained by the object in the View using foreach.

The following code is what I'm I'm trying to do. But its not working.

The Controller:

public ActionResult Students()
{
    String connectionString = "<THE CONNECTION STRING HERE>";
    String sql = "SELECT * FROM students";
    SqlCommand cmd = new SqlCommand(sql, connectionString);

    using(SqlConnection connectionString = new SqlConnection(connectionString))
    {
        connectionString.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
    }

    ViewData.Add("students", rdr);

    return View();
}

The View:

<h1>Student</h1>

<table>
    <!-- How do I display the records here? -->
</table>

解决方案

1. First create a Model that will hold the values of the record. for instance:

public class Student
{
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public string Class {get;set;}
    ....
}

2. Then load the rows from your reader to a list or something:

public ActionResult Students()
{
    String connectionString = "<THE CONNECTION STRING HERE>";
    String sql = "SELECT * FROM students";
    SqlCommand cmd = new SqlCommand(sql, conn);

    using(SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        var model = new List<Student>();
        while(rdr.Read())
        {
            var student = new Student();
            student.FirstName = rdr["FirstName"];
            student.LastName = rdr["LastName"];
            student.Class = rdr["Class"];
            ....

            model.Add(student);
        }

    }

    return View(model);
}

3. Lastly in your View, declare the kind of your model:

@model List<Student>

<h1>Student</h1>

<table>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Class</th>
    </tr>
    @foreach(var student in Model)
    {
    <tr>
        <td>@student.FirstName</td>  
        <td>@student.LastName</td>  
        <td>@student.Class</td>  
    </tr>
    }
</table>

这篇关于如何显示asp.net mvc的观点数据库记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆