views:

1226

answers:

1

Hi,

I am migrating my application from asp.net mvc to mvc version 2 and am having the following issue.

I have paging links << < > >> that I include in each page. Like so:

        <% Html.RenderPartial("PagingControl", Model); %>

They exist in an ascx file as follows.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BankingDB.Controllers.Utility.IPagedSortedObject>" %>
    <div class="paging">
        <div class="previous-paging">
<!- error!! -><%= Model.HasPreviousPage ? Html.ActionLink("<<", "Index", Model.buildParams(1)) : "<<"%>
            <%= Model.HasPreviousPage ? Html.ActionLink("<", "Index", Model.buildParams(Model.PreviousPageIndex)) : "<"%>
        </div>
        <div class="paging-details">
            Showing records <%= Model.BaseRecordIndex %> to <%= Model.MaxRecordIndex %> of <%= Model.TotalRecordCount %>
        </div>
        <div class="next-paging">
            <%= Model.HasNextPage ? Html.ActionLink(">", "Index", Model.buildParams(Model.NextPageIndex)) : ">"%>
            <%= Model.HasNextPage ? Html.ActionLink(">>", "Index", Model.buildParams(Model.PageCount)) : ">>"%>
        </div>        
    </div>

When I try to access the page I get the error:

CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Web.Mvc.MvcHtmlString' and 'string'

The error is marked above and appears to be with the action link. Including the controller name doesn't help. Any ideas?

+5  A: 

Html.ActionLink() now returns an MvcHtmlString, instead of just a string. This new class derives from IHtmlString. You cannot automatically cast from string to an IHtmlString.

Model.HasPreviousPage ? Html.ActionLink("<<", "Index", Model.buildParams(1)) : "<<"

needs to change to

Model.HasPreviousPage ? Html.ActionLink("<<", "Index", Model.buildParams(1)) : new MvcHtmlString.Create("<<")

This new class will be used in ASP.NET 4 to make "smart" use of the new ASP escape operator.

<%: Html.ActionLink("My Link", "Action", "Controller") %> <!-- knows to not HTML-escape, because ActionLink is an IHtmlString -->
<%: Model.FirstName %> <!-- short hand notation for <%= Html.Escape(Model.FirstName) %> -->
Jarrett Meyer
Thank you very much. Note that it should be MvcHtmlString.Create("<<") though since you can't create a new MvcHtmlString using the constructor.
Alistair
Just a note that you can also append `.ToString()` to the `Html.ActionLink(...)` in order to get a proper string. In which case, you wouldn't need to create a new MvcHtmlString for the "<<" on the false side of that ternary...
Funka