Neovssmith
Neovssmith

Reputation: 83

How can I highlight current selection of ActionLink in asp.net c#

So I have an action link that handle's my pagination. I want to style it but my solution is ugly with repeating code. There's another reason more than vanity for me to want to simplify the code. Here's my pagination:

Page:
@for (int i = 1; i <= Model.PagingInfo.TotalPages; i++)
{
    if (i == Model.PagingInfo.CurrentPage)
    {
        @Ajax.ActionLink(i.ToString(), "ActivityManagementTwo", "CallCenter",  new { page = i, timeFilter = Model.DateFilter.timeFilter, dateStart = Model.DateFilter.dateStart,
       dateEnd= Model.DateFilter.dateEnd, districtId = Model.DateFilter.districtId, pageSize = Model.PagingInfo.ItemsPerPage
        }, new AjaxOptions { UpdateTargetId = "activitiesForPartial" }, new { @class = "paginateRyan active" })
    }
    else
    {
        @Ajax.ActionLink(i.ToString(), "ActivityManagementTwo", "CallCenter",  new { page = i, timeFilter = Model.DateFilter.timeFilter, dateStart = Model.DateFilter.dateStart,
        dateEnd= Model.DateFilter.dateEnd, districtId = Model.DateFilter.districtId, pageSize = Model.PagingInfo.ItemsPerPage
         }, new AjaxOptions { UpdateTargetId = "activitiesForPartial" }, new { @class = "paginateRyan" })
    }
}

my page info model looks like

public class PagingInfoModel
{
    public int TotalItems { get; set; }
    public int ItemsPerPage { get; set; }
    public int CurrentPage { get; set; }
    public int TotalPages =>
    (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage);
}

Is there a more elegant way to add a property to the class like class = "paginate active" when the model.PagingInfo.CurrentPage == i ?

Upvotes: 1

Views: 40

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You can use a ternary operator inside the ActionLink

@Ajax.ActionLink(i.ToString(), "ActivityManagementTwo", "CallCenter", 
    new AjaxOptions { UpdateTargetId = "activitiesForPartial" }, 
    new { @class = i == Model.PagingInfo.CurrentPage ? "paginateRyan active" : "paginateRyan" })

Upvotes: 2

Related Questions