从数据集中传递值到视图

时间:2019-04-10 08:21:16

标签: asp.net-mvc vb.net dataset

我有一个简单的程序,可以从数据库中的表中获取值并将其显示在视图中。我正在使用vb.net和MVC。

我都是这方面的初学者,但没有得到任何结果。该值存储在我认为的数据表中,但是我无法将其传递给视图,也无法显示它。 我尝试了从网上获得的代码,但无法正常工作。它们大多数都在C#中,但是我使用在线转换器将代码转换为vb.net,结果得到的代码不正确。

  

Employee.vb(模型类)

Public Class Employee

    Private _id As Integer
    Public Property id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _firstname As String
    Public Property firstname() As String
        Get
            Return _firstname
        End Get
        Set(ByVal value As String)
            _firstname = value
        End Set
    End Property

    Private _lastname As String
    Public Property lastname() As String
        Get
            Return _lastname
        End Get
        Set(ByVal value As String)
            _lastname = value
        End Set
    End Property

End Class

  

Operations.vb(从DB获得价值)

Public Shared Function GetDataToDataSet() As DataSet
        Dim con As Connection = New Connection()
        Dim cnn = con.ConnectionSetup()

        Dim query As String = "use DemoDb select * from employee_db"
        Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, cnn)
        Dim ds As DataSet = New DataSet
        adapter.Fill(ds)
        Return ds

    End Function
  

HelpController

 Public Class HelpController
        Inherits Controller

 Function Home() As ActionResult
            Dim ds As New DataSet
            ds = DataOperations.Operations.GetDataToDataSet()

            Return View(ds)

        End Function
  End Class
  

Home.vbhtml(查看以显示代码)

@imports System.Data
@ModelType DataSet

@Code
    ViewData("Title") = "Home"
End Code

<h2>Home</h2>

<table>
    <thead>
        <tr>           
           @For Each col As DataColumn In Model.Tables[0].Columns
               @<th>@col.ColumnName</th>
           Next

        </tr>
    </thead>
    <tbody>

        @For Each row As DataRow In Model.Tables[0].Rows
            @<tr>
                <td>@row["Id"]</td>
                <td>@row["firstname"]</td>
                <td>@row["lastname"]</td>    
            </tr>



        Next
    </tbody>
</table>

我真的很帮忙。我知道这是一个简单的程序,但我是vb.net的新手,因此使它变得更加困难。期待获得帮助。

0 个答案:

没有答案