1

我刚开始使用asp.net mvc ..因为我不熟悉它,我只想问一个关于actionlink html helper的问题..

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

如果我单击操作链接,我希望它会将我呈现到这个网址:/Administration/NewsPublic/7 但它给了我这个网址:/Home/NewsPublic?Length=14

actionlink 是否仅在同一个控制器中传递 id?

先感谢您!

4

2 回答 2

4

要呈现指向/Administration/NewsPublic/7的链接,您应该使用

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

第五个参数让编译器选择

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

扩展方法重载而不是

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

并且不要忘记添加参数分配

New With {.id = 7}

代替

New With {.id}
于 2009-09-14T05:07:59.897 回答
1

默认情况下,Html.ActionLink 将使用当前控制器。但是有十几个 ActionLink() 的重载,并且有多个版本可以接受控制器参数。尝试:

Html.ActionLink("Read More...", 
                 "NewsPublic", 
                 "Administration",
                 New With { .id = id },
                 null)
于 2009-09-14T04:11:01.703 回答