MVC application does not start using the generic method

I wanted to create a method in my base controller that would take a list and return it as a SelectList. I wrote a method and the site compiles, but I get this message:

It is not possible to call the action method 'System.Collections.Generic.IEnumerable 1[System.Web.Mvc.SelectListItem] GetSelectList[T](System.Collections.Generic.IEnumerable1 [T], System.String, System.String, System.String, System.Object)' on the controller 'PublicationSystem.Controllers.BaseController' because the action method is common method.
Parameter Name: methodInfo

I wonder what I did wrong. Here is the code:

public partial class BaseController : Controller
{
    public IEnumerable<SelectListItem> GetSelectList<T>
    (
        IEnumerable<T> itemList, 
        string textField,
        string valueField,
        string defaultPrompt = "", 
        object defaultValue = null)
    {
        IEnumerable<SelectListItem> returnList = null;

        if (!string.IsNullOrEmpty(defaultPrompt))
        {
            returnList = Enumerable.Repeat(
                new SelectListItem { Value = (string)defaultValue, Text = defaultPrompt },
                count: 1);
        }

        var textProp = typeof (T).GetProperty(textField);
        var valueProp = typeof (T).GetProperty(valueField);

        returnList = returnList.Concat
            (itemList
                .Select(x =>
                    new SelectListItem
                    {
                        Value = Convert.ToString(valueProp.GetValue(x)),
                        Text = Convert.ToString(textProp.GetValue(x)),
                    }).Distinct().ToList());

        return returnList.ToList();
    }
}

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.LowercaseUrls = true;
        routes.MapMvcAttributeRoutes(); // Errors here
        //..
    }
}
+4
source share
2 answers

public . Asp.Net .

. .

( ). protected.

+11

. IEnumerable<T>. :

public static class EnumerableExtensions
{
    public static IEnumerable<SelectListItem> AsSelectList<T>
    (
        this IEnumerable<T> dataList,
        string textField,
        string valueField,
        string defaultPrompt = "",
        object defaultValue = null)
    {
        IEnumerable<SelectListItem> returnList = new List<SelectListItem>();

        if (!string.IsNullOrEmpty(defaultPrompt))
        {
            returnList = Enumerable.Repeat(
                new SelectListItem { Value = (string)defaultValue, Text = defaultPrompt },
                count: 1);
        }

        var textProp = typeof(T).GetProperty(textField);
        var valueProp = typeof(T).GetProperty(valueField);

        returnList = returnList.Concat
            (dataList
                .Select(x =>
                    new SelectListItem
                    {
                        Value = Convert.ToString(valueProp.GetValue(x)),//x["valueField"],
                        Text = Convert.ToString(textProp.GetValue(x)),//x.["textField"]
                    }).Distinct().ToList());

        return returnList;
    }
}
0

All Articles