Search code examples
c#asp.net-mvc-3genericsreflectionnested-generics

Lost in the generics and reflection sauce


I am trying to build an MVC helper for building a MultiSelectList with my own sort of parameters. I am basing it off of a SelectFor helper I built a while back. The SelectFor looks like this:

    public delegate object Property<T>(T property);
    public delegate object Property<T, K>(T property, K propertyKey);

    public static HtmlString SelectFor<TModel, TProperty, TListItem>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> forExpression,
        IEnumerable<TListItem> enumeratedItems,
        Property<TListItem> idExpression,
        Property<TListItem> displayExpression,
        Property<TListItem> titleExpression,
        object htmlAttributes,
        bool blankFirstLine) where TModel : class
    {
        //initialize values
        var metaData = ModelMetadata.FromLambdaExpression(forExpression, htmlHelper.ViewData);
        var propertyName = metaData.PropertyName;
        var propertyValue = htmlHelper.ViewData.Eval(propertyName).ToStringOrEmpty();
        var enumeratedType = typeof(TListItem);

        //build the select tag
        var returnText = string.Format("<select id=\"{0}\" name=\"{0}\"", HttpUtility.HtmlEncode(propertyName));
        if (htmlAttributes != null)
        {
            foreach (var kvp in htmlAttributes.GetType().GetProperties()
             .ToDictionary(p => p.Name, p => p.GetValue(htmlAttributes, null)))
            {
                returnText += string.Format(" {0}=\"{1}\"", HttpUtility.HtmlEncode(kvp.Key),
                 HttpUtility.HtmlEncode(kvp.Value.ToStringOrEmpty()));
            }
        }
        returnText += ">\n";

        if (blankFirstLine)
        {
            returnText += "<option value=\"\"></option>";
        }

        //build the options tags
        foreach (TListItem listItem in enumeratedItems)
        {
            var idValue = idExpression(listItem).ToStringOrEmpty();
            var displayValue = displayExpression(listItem).ToStringOrEmpty();
            var titleValue = titleExpression(listItem).ToStringOrEmpty();
            returnText += string.Format("<option value=\"{0}\" title=\"{1}\"",
                HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue));
            if (idValue == propertyValue)
            {
                returnText += " selected=\"selected\"";
            }
            returnText += string.Format(">{0}</option>\n", displayValue);
        }

        //close the select tag
        returnText += "</select>";
        return new HtmlString(returnText);
    }

The MultiSelectFor is only slightly different, with one difference of significance: the forExpression will be a generic IEnumerable of the property type of the idExpression. This collection will be used to "pre-select" the list items and will be the return value for the selected items on the form. I have (I think) gotten further with this, but still quite lost.

    public static HtmlString MultiSelectListFor<TModel, TProperty, TProperty, TPropertyKey>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, IEnumerable<TPropertyKey>>> forExpression,
        IEnumerable<TProperty> enumeratedItems,
        Property<TProperty, TPropertyKey> idExpression,
        Property<TProperty> displayExpression,
        Property<TProperty> titleExpression,
        object htmlAttributes) where TModel : class
    {
        //initialize values
        var metaData = ModelMetadata.FromLambdaExpression(forExpression, htmlHelper.ViewData);
        var propertyName = metaData.PropertyName;
        var propertyValue = htmlHelper.ViewData.Eval(propertyName).ToStringOrEmpty();
        var enumeratedType = typeof(TProperty);

        //build the select tag
        var returnText = string.Format("<select multiple=\"multiple\" id=\"{0}\" name=\"{0}\"", HttpUtility.HtmlEncode(propertyName));
        if (htmlAttributes != null)
        {
            foreach (var kvp in htmlAttributes.GetType().GetProperties()
             .ToDictionary(p => p.Name, p => p.GetValue(htmlAttributes, null)))
            {
                returnText += string.Format(" {0}=\"{1}\"", HttpUtility.HtmlEncode(kvp.Key),
                 HttpUtility.HtmlEncode(kvp.Value.ToStringOrEmpty()));
            }
        }
        returnText += ">\n";

        //build the options tags
        foreach (TProperty listItem in enumeratedItems)
        {
            //this part here needs to change:
            var idValue = ???.ToStringOrEmpty();

            var displayValue = displayExpression(listItem).ToStringOrEmpty();
            var titleValue = titleExpression(listItem).ToStringOrEmpty();
            returnText += string.Format("<option value=\"{0}\" title=\"{1}\"",
                HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue));
            if (propertyValue.Contains(idValue))
            {
                returnText += " selected=\"selected\"";
            }
            returnText += string.Format(">{0}</option>\n", displayValue);
        }

        //close the select tag
        returnText += "</select>";
        return new HtmlString(returnText);
    }

Help is appreciated.

UPDATE Thanks for the answer! Generics get confusing to me sometimes. The complete solution is as follows:

    public delegate object Property<T>(T property);

    public static HtmlString MultiSelectListFor<TModel, TKey, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, IEnumerable<TKey>>> forExpression,
        IEnumerable<TProperty> enumeratedItems,
        Func<TProperty, TKey> idExpression,
        Property<TProperty> displayExpression,
        Property<TProperty> titleExpression,
        object htmlAttributes) where TModel : class
    {
        //initialize values
        var metaData = ModelMetadata.FromLambdaExpression(forExpression, htmlHelper.ViewData);
        var propertyName = metaData.PropertyName;
        var propertyValue = htmlHelper.ViewData.Eval(propertyName).ToStringOrEmpty();
        var enumeratedType = typeof(TProperty);

        //build the select tag
        var returnText = string.Format("<select multiple=\"multiple\" id=\"{0}\" name=\"{0}\"", HttpUtility.HtmlEncode(propertyName));
        if (htmlAttributes != null)
        {
            foreach (var kvp in htmlAttributes.GetType().GetProperties()
             .ToDictionary(p => p.Name, p => p.GetValue(htmlAttributes, null)))
            {
                returnText += string.Format(" {0}=\"{1}\"", HttpUtility.HtmlEncode(kvp.Key),
                 HttpUtility.HtmlEncode(kvp.Value.ToStringOrEmpty()));
            }
        }
        returnText += ">\n";

        //build the options tags
        foreach (TProperty listItem in enumeratedItems)
        {
            var idValue = idExpression(listItem).ToStringOrEmpty();
            var displayValue = displayExpression(listItem).ToStringOrEmpty();
            var titleValue = titleExpression(listItem).ToStringOrEmpty();
            returnText += string.Format("<option value=\"{0}\" title=\"{1}\"",
                HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue));
            if (propertyValue.Contains(idValue))
            {
                returnText += " selected=\"selected\"";
            }
            returnText += string.Format(">{0}</option>\n", displayValue);
        }

        //close the select tag
        returnText += "</select>";
        return new HtmlString(returnText);
    }

    public static HtmlString MultiSelectListFor<TModel, TKey, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, IEnumerable<TKey>>> forExpression,
        IEnumerable<TProperty> enumeratedItems,
        Func<TProperty, TKey> idExpression,
        Property<TProperty> displayExpression,
        Property<TProperty> titleExpression) where TModel : class
    {
        return htmlHelper.MultiSelectListFor(forExpression, enumeratedItems, idExpression, displayExpression, titleExpression, null);
    }

Thanks again for the help!


Solution

  • It sounds like you're asking for Expression<Func<TModel, IEnumerable<TListItem>>.

    You need to make a separate generic parameter and use it for both the ID property and the for expression:

    public static HtmlString MultiSelectListFor<TModel, TKey, TListItem>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, IEnumerable<TKey>>> forExpression,
        IEnumerable<TListItem> enumeratedItems,
        Func<TListItem, TKey> idExpression,