views:

286

answers:

2
+1  Q: 

html.actionlink

hi everyone.. i'm just starting to use asp.net mvc.. since i'm not familiar with it, i just want to ask a question about actionlink html helper..

i have this code in my index.aspx home view..

    <%  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%>

if i click the actionlink, i was expecting it will render me to this url: /Administration/NewsPublic/7 but rather it gives me this url : /Home/NewsPublic?Length=14

does actionlink pass id in the same controller only?

thank you in advance!

+1  A: 

By default, Html.ActionLink will use the current controller. But there are about a dozen overloads of ActionLink(), and there are multiple versions of it that will accept a controller parameter. Try:

Html.ActionLink("Read More...", 
                 "NewsPublic", 
                 "Administration",
                 New With { .id = id },
                 null)
womp
thank you for your response..um,it gives me this error: Name of field or property being initialized in an object initializer must start with '.'.i tried to put . in the "id" but it says identifier expected..
tiff
Yeah sorry... VB initializers are trickier than in C#, I probably didn't get it right. Try "New With { .id = id }".
womp
the one you edited worked!=)thank you!
tiff
Great. I'll update my answer text with the corrected initializer.
womp
A: 

To render link to /Administration/NewsPublic/7 you should use

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

Fifth parameter makes compiler to choose

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

extension method overload instead of

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

And don't forget to add parameter assignment

New With {.id = 7}

instead of

New With {.id}
Alexander Prokofyev