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.