In a view, I have a list of data that is loaded into a table (grid type) and inside it I need to call a controller method to get more data through a url.action
. This is how I have the data in the view:
<% For Each item In Model.listadoNoticias%>
<tr>
<td>
<%: Html.DisplayFor(Function(modelItem) item.TituloNoticia)%>
</td>
<td>
<%: Html.DisplayFor(Function(modelItem) item.DescripcionNoticia)%>
</td>
<td>
<%: Url.Action("Datos", "Noticias", New With {.id = item.IdNoticia})%>'
</td>
</tr><% Next%>
In the url action to the controller so that data that comes from a query appears. In the controller:
Function Datos (ByVal id As Integer) As ActionResult
Using db As New BD_Prueba
Dim datos = (From t3 In db.FOTO From intermedia In t3.NOTICIA.Where(Function(x) x.IdNoticia = id) Select New With {t3.NombreFoto}).ToList()
End Using
Return View(datos)
End Function
I imagine that instead of go it ActionResult
should be something like List(Of String)
and instead of Return View(datos)
, it should be something like Return list(fotos)
. I would like to know what this fix would look like in the controller, since I need to return a list of data to the action url of the view.
And I would like to add another question, if instead of returning a list of strings, I had to return a list of binary files, how would it be in that case too?
If what you want is to inject a string or html into the table, you should not use the
Url.Action()
, but it should be theHtml.Action()
:In this way, what the action returns will be rendered in that place.
You can make the action return a simple string if you need to, although I notice you create a generated list from linq, you may need to convert it, if you have a list of strings you could use the
String.Join()
to concatenate the values separated by a characterA clarification, remember that there is the
Html.Action()
and theHtml.RenderAction()
Html.RenderAction and Html.Action
The use of the string.join could be like