且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何显示asp.net mvc的观点数据库记录

更新时间:2023-02-16 11:37:31

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



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

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



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

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

model.Add(学生);
}

}

返回查看(模型);
}



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



  @model名单&LT;的Student GT; 

&LT; H1&GT;学生&LT; / H1&GT;

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


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>