@Html.ActionLink one works and one does not

纵然是瞬间 提交于 2019-12-13 00:08:32

问题


I have two @Html.ActionLink's one works and one does not and I cannot figure out why?

They are both within the same controller: StoreController and both call ActionResults within different controllers, this is the one that works and calls the Index function within the Director Class:

<ul>
@foreach (var item in Model)
{
    <li>
    <h2>@item.Title</h2>
    </li>
    <li class = "lihead">
    Directed by 
    @Html.ActionLink((string)item.Director.Name, "Index", "Director", new { searchString = item.Director.Name }, null)
    </li>
    <li>
    <i>@item.Synopsis</i>
    </li> 
    <li class = "lihead">
    Price per ticket £
    @item.Price
    </li>        
}

This is the one that does not work:

 @foreach (var item in Model) {
        @Html.ActionLink("Check Availability", "CheckAvail", "Booking", new { id = item.ShowId })

}

This one calls within the StoreController when I want it to call the CheckAvail function within the BookingController. However it does work when I amend it as so:

 @foreach (var item in Model) {
        @Html.ActionLink("Check Availability", "CheckAvail", "Booking")}

But I need it to take through the ShowId to the function???


回答1:


Use it like this

@Html.ActionLink("Check Availability", "CheckAvail", "Booking", new { @id = item.ShowId },null)

It is using this overload

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)

http://msdn.microsoft.com/en-us/library/dd504972.aspx

the last parameter is the HTML attribute. If you have some HTML atribute to pass, you pass it there instead of null

@Html.ActionLink("Check Availability", "CheckAvail", "Booking", new { @id = item.ShowId },new { @class="myCssClass"} )


来源:https://stackoverflow.com/questions/10298823/html-actionlink-one-works-and-one-does-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!