我刚刚开始使用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%>

如果我点击actionlink,我希望它能让我看到这个网址: /行政/ NewsPublic / 7 但它给了我这个网址: /主页/ NewsPublic?长度= 14

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

提前谢谢你!

有帮助吗?

解决方案

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

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

其他提示

要呈现指向 / 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}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top