Pregunta

estoy empezando a usar asp.net mvc .. como no estoy familiarizado con él, solo quiero hacer una pregunta sobre actionlink html helper ..

Tengo este código en mi vista de inicio index.aspx ..

    <%  Dim _news As datatable = ViewData.Model%>
    <%  For count As Integer = 0 To _news.Rows.Count - 1%>
    <%  Dim id As Integer = _news.Rows(count).Item("IDnews")%>
    <%=_news.Rows(count).Item("newsTitle")%>
    <p>
    <%=_news.Rows(count).Item("newsContent")%><br />
    <%=Html.ActionLink("Read More..", "NewsPublic", "Administration", New With {id})%>
    </p>
    <%Next%>

si hago clic en el enlace de acción, esperaba que me mostrara esta url: / Administración / NewsPublic / 7 sino que me da esta url: / Inicio / NewsPublic? Length = 14

¿actionlink pasa la identificación solo en el mismo controlador?

gracias de antemano!

¿Fue útil?

Solución

Por defecto, Html.ActionLink usará el controlador actual. Pero hay alrededor de una docena de sobrecargas de ActionLink (), y hay varias versiones que aceptarán un parámetro del controlador. Prueba:

Html.ActionLink("Read More...", 
                 "NewsPublic", 
                 "Administration",
                 New With { .id = id },
                 null)

Otros consejos

Para representar el enlace a / Administration / NewsPublic / 7 debe usar

<%=Html.ActionLink("Read More..", "NewsPublic", "Administration", 
    New With {.id = 7}, Nothing)%>

El quinto parámetro hace que el compilador elija

ActionLink(string linkText, string actionName, string controllerName, 
    object routeValues, object htmlAttributes)

sobrecarga del método de extensión en lugar de

ActionLink(string linkText, string actionName, object routeValues, 
    object htmlAttributes)

Y no olvide agregar la asignación de parámetros

New With {.id = 7}

en lugar de

New With {.id}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top