Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 actionlink breaking after migration from MVC version 1

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?

like image 396
Alistair Avatar asked Feb 16 '10 23:02

Alistair


1 Answers

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)) : 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) %> -->
like image 132
Jarrett Meyer Avatar answered Oct 07 '22 00:10

Jarrett Meyer