0

I want to create a custom HTML Helper where I can pass a LINQ expression as a parameter, like this:

@Html.GetBackgroundColor(model => model.RiskAssessment)

I want to use it to display some custom css in an MVC view, depending on what the RiskAssessment property is.

So I created a helper method like this:

    public static string GetBackgroundColor<T, TResult>(this HtmlHelper<T> htmlHelper, Expression<Func<T, TResult>> expression)
    {
        ...
    }

However, that won't compile, the error is IEnumerable does not contain a definition for 'RiskAssessment' So I changed the method to

 public static string GetBackgroundColor<T, TResult>(this HtmlHelper<IEnumerable<T>> htmlHelper, Expression<Func<T, TResult>> expression)
{
    ...
}

which compiles, so now I presumably have all the objects in the collection but I have no idea how to get the object I want as I can't use use the expression on the IEnumerable, there is no Where() method available. I would have thought I could do something like this:

IEnumerable<T> collection = htmlHelper.ViewData.Model;
T obj = collection.Where(expression)

but I just don't know what I am doing wrong here.

ardmark
  • 81
  • 1
  • 9
  • What do you want `GetBackgroundColor` to render? –  Oct 25 '18 at 21:19
  • What is the type of `RiskAssessment`? In which way the CSS settings are applied, can you give example for expected results? – Tetsuya Yamamoto Oct 26 '18 at 02:35
  • I am displaying an EntityFramework table in an MVC view, using the MVC-generated script. I want to set the color of a table row, based on the property that row displays. The RiskAssessment property is just a bool, so I would like to add an inline style to the table row based on whether that property is true or false (i.e. set background to green or red), and I would return the style from the HTML helper method, but the problem I have is I just don't see how to get the model property in the HTML helper method. – ardmark Oct 26 '18 at 08:57
  • Figured it out, simple mistake. The table header row is set up with @Html.DisplayNameFor(model => modelType), and I was trying to call my custom HTML helper with these parameters. I should have been calling the method on each table row, using @Html.GetBackgroundColor(modelItem => item.RiskAssessment), and this works because I can use htmlHelper.ValueFor(expression) within the method to get the property value. – ardmark Oct 26 '18 at 09:37

1 Answers1

0

Figured it out, simple mistake. The table header row is set up with @Html.DisplayNameFor(model => modelType), and I was trying to call my custom HTML helper with these parameters. I should have been calling the method on each table row, using @Html.GetBackgroundColor(modelItem => item.RiskAssessment), and this works because I can use htmlHelper.ValueFor(expression) within the method to get the property value. That said, I have no idea how the header row is generated as Html.DisplayNameFor uses the same method signature as my custom method but Intellisense reports that one of the Expression types is unknown. But that is not an issue for me. Thanks.

ardmark
  • 81
  • 1
  • 9