Skip to main content
se añadieron 239 caracteres en el cuerpo
Origen Enlace
Leandro Tuttini
  • 36k
  • 3
  • 15
  • 41

No deberias programar el accesoacceder a los datos desde la view, para eso existen los es por medio del controller y es en el action que debes conectarte aconsultas la db y devuelves un model a la view

Podrias definir una clase modelmodel como ser

Public Class CarouselViewModel
    Public Property Images As List(Of Imagen)
End Class

Public Class Imagen
    Public Property Nombre As String
    Public Property PathUrl As String
    Public Property Counter As Integer
End Class
Public Class xxController

   Public Function Index() As ActionResult
   
        Dim model As New CarouselViewModel()
        model.Images = new List(Of Imagen)
        
        Dim cn = ConfigurationManager.ConnectionStrings("portal").ConnectionString

        Dim conexion As New SqlConnection(cn)
        Dim cmd As New SqlCommand()
        cmd.Connection = conexion
        cmd.CommandText = "SELECT * FROM imagenes WHERE status='1' ORDER BY datecreate "
        conexion.Open()       

        Dim dr As SqlDataReader = cmd.ExecuteReader()

        Dim i As Integer = 0
        Do While dr.Read()
            Dim img As New Imagen()
            img.Nombre = dr.GetString(3)
            img.PathUrl = dr.GetString(1)
            img.Counter = i
            model.Images.Add(img)
            i= i + 1
        Loop

        dr.Close()
        conexion.Close()
        
        Return View(model)

   End Function

End Class
@Model CarouselViewModel
@Code
   PageData("Title") = "Escriba el título aquí"
   Layout = "_Layout.vbhtml"

End Code
<div> 
    <div id="page-wrapper">
        <div id="page-inner">
            <section class="section-white">
                <div class="container">
                    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <!-- Indicadores -->
                        <ol class="carousel-indicators">
                            @For Each item In Model.Imagenes
                                <li data-target='#carousel-example-generic' data-slide-to='@item.Counter'></li>"
                                <div class='item active'><img src='@item.Path'Url' /><div class='carousel-caption'><h3>@item.Nombre</h3></div></div>"
                            End If
                        </ol>
                        <div class="carousel-inner">
                        <!-- Wrapper for slides -->
                           
                        </div>
                        <!-- Controles -->
                        <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left"></span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
                            <span class="glyphicon glyphicon-chevron-right"></span>
                        </a>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>

Analiza como se realziarealiza el For Each de la lista ende imagenes que contiene el model tomando, usas las propeidades y armandopropiedades para arman los tag html. Es una Url lo que debes asignar el src de la imagen.

No deberias programar el acceso a datos desde la view, para eso existen los controller y es en el action que debes conectarte a la db

Podrias definir una clase model

Public Class CarouselViewModel
    Public Property Images As List(Of Imagen)
End Class

Public Class Imagen
    Public Property Nombre As String
    Public Property Path As String
    Public Property Counter As Integer
End Class
Public Class xxController

   Public Function Index() As ActionResult
   
        Dim model As New CarouselViewModel()
        model.Images = new List(Of Imagen)
        
        Dim cn = ConfigurationManager.ConnectionStrings("portal").ConnectionString

        Dim conexion As New SqlConnection(cn)
        Dim cmd As New SqlCommand()
        cmd.Connection = conexion
        cmd.CommandText = "SELECT * FROM imagenes WHERE status='1' ORDER BY datecreate "
        conexion.Open()       

        Dim dr As SqlDataReader = cmd.ExecuteReader()

        Dim i As Integer = 0
        Do While dr.Read()
            Dim img As New Imagen()
            img.Nombre = dr.GetString(3)
            img.Path = dr.GetString(1)
            img.Counter = i
            model.Images.Add(img)
            i= i + 1
        Loop

        dr.Close()
        conexion.Close()
        
        Return View(model)

   End Function

End Class
<div> 
    <div id="page-wrapper">
        <div id="page-inner">
            <section class="section-white">
                <div class="container">
                    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <!-- Indicadores -->
                        <ol class="carousel-indicators">
                            @For Each item In Model.Imagenes
                                <li data-target='#carousel-example-generic' data-slide-to='@item.Counter'></li>"
                                <div class='item active'><img src='@item.Path' /><div class='carousel-caption'><h3>@item.Nombre</h3></div></div>"
                            End If
                        </ol>
                        <div class="carousel-inner">
                        <!-- Wrapper for slides -->
                           
                        </div>
                        <!-- Controles -->
                        <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left"></span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
                            <span class="glyphicon glyphicon-chevron-right"></span>
                        </a>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>

Analiza como se realzia el For Each de la lista en el model tomando las propeidades y armando los tag html

No deberias acceder a los datos desde la view, es por medio del controller y el action que consultas la db y devuelves un model a la view

Podrias definir una clase model como ser

Public Class CarouselViewModel
    Public Property Images As List(Of Imagen)
End Class

Public Class Imagen
    Public Property Nombre As String
    Public Property Url As String
    Public Property Counter As Integer
End Class
Public Class xxController

   Public Function Index() As ActionResult
   
        Dim model As New CarouselViewModel()
        model.Images = new List(Of Imagen)
        
        Dim cn = ConfigurationManager.ConnectionStrings("portal").ConnectionString

        Dim conexion As New SqlConnection(cn)
        Dim cmd As New SqlCommand()
        cmd.Connection = conexion
        cmd.CommandText = "SELECT * FROM imagenes WHERE status='1' ORDER BY datecreate "
        conexion.Open()       

        Dim dr As SqlDataReader = cmd.ExecuteReader()

        Dim i As Integer = 0
        Do While dr.Read()
            Dim img As New Imagen()
            img.Nombre = dr.GetString(3)
            img.Url = dr.GetString(1)
            img.Counter = i
            model.Images.Add(img)
            i= i + 1
        Loop

        dr.Close()
        conexion.Close()
        
        Return View(model)

   End Function

End Class
@Model CarouselViewModel
@Code
   PageData("Title") = "Escriba el título aquí"
   Layout = "_Layout.vbhtml"

End Code
<div> 
    <div id="page-wrapper">
        <div id="page-inner">
            <section class="section-white">
                <div class="container">
                    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <!-- Indicadores -->
                        <ol class="carousel-indicators">
                            @For Each item In Model.Imagenes
                                <li data-target='#carousel-example-generic' data-slide-to='@item.Counter'></li>"
                                <div class='item active'><img src='@item.Url' /><div class='carousel-caption'><h3>@item.Nombre</h3></div></div>"
                            End If
                        </ol>
                        <div class="carousel-inner">
                        <!-- Wrapper for slides -->
                           
                        </div>
                        <!-- Controles -->
                        <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left"></span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
                            <span class="glyphicon glyphicon-chevron-right"></span>
                        </a>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>

Analiza como se realiza el For Each de la lista de imagenes que contiene el model, usas las propiedades para arman los tag html. Es una Url lo que debes asignar el src de la imagen.

Origen Enlace
Leandro Tuttini
  • 36k
  • 3
  • 15
  • 41

No deberias programar el acceso a datos desde la view, para eso existen los controller y es en el action que debes conectarte a la db

Podrias definir una clase model

Public Class CarouselViewModel
    Public Property Images As List(Of Imagen)
End Class

Public Class Imagen
    Public Property Nombre As String
    Public Property Path As String
    Public Property Counter As Integer
End Class

Despues desde el action del controller devuelves el model a la view

Public Class xxController

   Public Function Index() As ActionResult
   
        Dim model As New CarouselViewModel()
        model.Images = new List(Of Imagen)
        
        Dim cn = ConfigurationManager.ConnectionStrings("portal").ConnectionString

        Dim conexion As New SqlConnection(cn)
        Dim cmd As New SqlCommand()
        cmd.Connection = conexion
        cmd.CommandText = "SELECT * FROM imagenes WHERE status='1' ORDER BY datecreate "
        conexion.Open()       

        Dim dr As SqlDataReader = cmd.ExecuteReader()

        Dim i As Integer = 0
        Do While dr.Read()
            Dim img As New Imagen()
            img.Nombre = dr.GetString(3)
            img.Path = dr.GetString(1)
            img.Counter = i
            model.Images.Add(img)
            i= i + 1
        Loop

        dr.Close()
        conexion.Close()
        
        Return View(model)

   End Function

End Class

En la view defines la clase model y la recorres

<div> 
    <div id="page-wrapper">
        <div id="page-inner">
            <section class="section-white">
                <div class="container">
                    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <!-- Indicadores -->
                        <ol class="carousel-indicators">
                            @For Each item In Model.Imagenes
                                <li data-target='#carousel-example-generic' data-slide-to='@item.Counter'></li>"
                                <div class='item active'><img src='@item.Path' /><div class='carousel-caption'><h3>@item.Nombre</h3></div></div>"
                            End If
                        </ol>
                        <div class="carousel-inner">
                        <!-- Wrapper for slides -->
                           
                        </div>
                        <!-- Controles -->
                        <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left"></span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
                            <span class="glyphicon glyphicon-chevron-right"></span>
                        </a>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>

Analiza como se realzia el For Each de la lista en el model tomando las propeidades y armando los tag html