jimjim
jimjim

Reputation: 2503

Using Lambda Expressions to help in MVC razor View

Update : I am only looking for hint on how to implement IsHighlighted method

I have following type of code repeated:

<div class="@GetCssClass(Model.IsProperty1Highlighted)">@Model.Property1
<div class="@GetCssClass(Model.IsProperty2Highlighted)">@Model.Property2
<div class="@GetCssClass(Model.IsProperty3Highlighted)">@Model.Property3

Also having a IsPropertyXHighlighted property for each X is too ugly, It would be nice if I had a method to do something like this:

<div class="@GetCssClass(Model.IsHighlighted(Model => Model.Property1))">@Model.Property1
<div class="@GetCssClass(Model.IsHighlighted(Model => Model.Property2))">@Model.Property2
<div class="@GetCssClass(Model.IsHighlighted(Model => Model.Property3))">@Model.Property3

I think this is doable with expressions but I do not how. Any hints on how to do this or a cleaner way achieving a similar result?

For example : Lets say Proprty1 needs to be highlighted , because it is a datetime and it is Thursday, Property2 Needs to be highlighted because it is Boolean and false, Property 3 is integer and when it is even needs to be highlighted

Upvotes: 0

Views: 1509

Answers (1)

Tieson T.
Tieson T.

Reputation: 21239

I would assume your first bit looks like so:

public static class HtmlExtensions
{
    public static IHtmlString GetHighlightClass(this HtmlHelper htmlHelper, bool shouldHighlight)
    {
        return shouldHighlight ? MvcHtmlString.Create("highlight") : null;
    }
}

If the rules for when something should be highlighted are fixed, you might have another static class:

public class HighlightHelper
{
    public static bool IsHighlighted(object obj)
    {
        bool shouldHighlight = false;

        if(obj is DateTime)
        {
            var date = (DateTIme)obj;
            shouldHighlight = date != null && date.DayOfWeek == DayOfWeek.Thursday;
        }
        else if(obj is int)
        {
            var number = (int)obj;
            shouldHighlight = number % 2 == 0;
        }
        // et cetera...

        return shouldHighlight;   
    }
}

You would then be able to use something similar to your second guess:

<div class="@Html.GetHighlightClass(HighlightHelper.IsHighlighted(Model.Property1))">@Model.Property1</div>

You might be able to clean that up a little by moving the IsHighlighted check inside the HtmlHelper:

public static IHtmlString GetHighlightClass(this HtmlHelper htmlHelper, object obj)
{
    bool shouldHighlight = HighlightHelper.IsHighlighted(obj);
    return shouldHighlight ? MvcHtmlString.Create("highlight") : null;
}

And then your view might have this:

<div class="@Html.GetHighlightClass(Model.Property1)">@Model.Property1</div>

And, just to show it works, some tests:

using System;

using FluentAssertions;
using NUnit.Framework;

namespace YourNamespace.Tests
{
    [TestFixture]
    public class HighlightHelperTests
    {
        [Test]
        public void HighlightHelper_Int_Tests()
        {
            bool shouldHighlight = false;

            shouldHighlight = HighlightHelper.IsHighlighted(3);
            shouldHighlight.Should().BeFalse();

            shouldHighlight = HighlightHelper.IsHighlighted(14);
            shouldHighlight.Should().BeTrue();
        }

        [Test]
        public void HighlightHelper_DateTime_Tests()
        {
            bool shouldHighlight = false;

            shouldHighlight = HighlightHelper.IsHighlighted(new DateTime(2016, 7, 13));
            shouldHighlight.Should().BeFalse();

            shouldHighlight = HighlightHelper.IsHighlighted(new DateTime(2016, 7, 14));
            shouldHighlight.Should().BeTrue();
        }
    }
}

Upvotes: 1

Related Questions