How to create MVC Extension Method for Dynamic type

Lijin Durairaj

I want to create Extension methods for dynamic type, but for some or the other reason i get error while compiling the code. below is my code sample The method which i am using is

public List<State> getStates()
{

    return new List<State>(){
         new State{StateID="1",StateName="Tamil Nadu"},
         new State{StateID="2",StateName="Assam"},
         new State{StateID="3",StateName="Andra"},
         new State{StateID="4",StateName="bihar"},
         new State{StateID="5",StateName="Bengal"},            
    };
}

and my extension method is

public static class helper
{
     public static IEnumerable<SelectListItem> getIntoDropDownState(this List<dynamic> data,string textField,string valueField)
    {
        return data.Select(x => new SelectListItem
        {
            Text = x.textField,
            Value = x.valueField
        });
    }
}

My compilation error which i get is

Compiler Error Message: CS1928: 'System.Collections.Generic.List' does not contain a definition for 'getIntoDropDownState' and the best extension method overload 'LearnAuthentication.Controllers.helper.getIntoDropDownState(System.Collections.Generic.List, string, string)' has some invalid arguments

I want to use the Extension method like this, and I am facing error

 @Html.DropDownListFor(x => x.StateID, new LearnAuthentication.Controllers.Address().getStates().getIntoDropDownState("StateName", "StateID"), "Select State", 0)

can anyone help

Nkosi

That extension method in your example is not going to do what you describe. You could use reflection and generics.

public static class helper {
    public static IEnumerable<SelectListItem> getIntoDropDownState<T>(this List<T> data, string textField, string valueField) where T : class {
        var type = typeof(T);

        var textPropertyInfo = type.GetProperty(textField);
        var valuePropertyInfo = type.GetProperty(valueField);

        return data.Select(x => new SelectListItem {
            Text = (string)textPropertyInfo.GetValue(x),
            Value = (string)valuePropertyInfo.GetValue(x)
        });
    }
}

Here is a unit test for the extension method

[TestClass]
public class MyTestClass {
    [TestMethod]
    public void MyTestMethod() {
        //Arrange
        var data = getStates();

        //Act
        var result = data.getIntoDropDownState("StateName", "StateID");

        //Assert
        Assert.IsNotNull(result);
        var first = result.First();
        Assert.AreEqual(data[0].StateName, first.Text);
        Assert.AreEqual(data[0].StateID, first.Value);
    }

    public List<State> getStates() {

        return new List<State>(){
             new State{StateID="1",StateName="Tamil Nadu"},
             new State{StateID="2",StateName="Assam"},
             new State{StateID="3",StateName="Andra"},
             new State{StateID="4",StateName="bihar"},
             new State{StateID="5",StateName="Bengal"},            
        };
    }

    public class State {
        public string StateID { get; set; }
        public string StateName { get; set; }
    }
}

Or you could forego reflection and use System.Web.Mvc.SelectList which does the heavy reflection lifting for you

public static class helper {
    public static IEnumerable<System.Web.Mvc.SelectListItem> getIntoDropDownState<T>(this List<T> data, string textField, string valueField) where T : class {

        return new System.Web.Mvc.SelectList(data, valueField, textField);

    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create an extension method on IHtmlHelper<dynamic>

From Dev

How to create extension method for Enum type argument?

From Dev

How to create extension method for Enum type argument?

From Dev

How to create an extension method in TypeScript for 'Date' data type

From Dev

How To Create ToJsonAsync Extension Method?

From Dev

How to create an extension method for any type of Enum array in VB.Net

From Dev

How to work with Generic type members in extension method?

From Dev

How to create a linq extension method with Func<>?

From Dev

How to create the IQueryable version of IEnumerable extension method?

From Dev

how to create dynamic radio buttons in mvc razor

From Dev

How to use dynamic Type Hints in Laravel method

From Dev

How to create a delegate with a dynamic parameter type?

From Dev

Extension Method On Casted Type

From Dev

Extension Method with Generic Type

From Dev

Call method of dynamic type

From Dev

Extension method call not working in MVC

From Dev

How to Write Generic Extension Method to Convert Type in C#

From Dev

How to write an extension method to return the type being used?

From Dev

How can I get the generic type parameter of the 'this' parameter in an extension method?

From Dev

How can I get the generic type parameter of the 'this' parameter in an extension method?

From Dev

How to write an extension method to return the type being used?

From Dev

How to create a generic Task.ContinueWith extension method

From Dev

How to create an HtmlHelper extension method that will bind an IEnumerable<T> to a table

From Dev

ASP.Net MVC Dynamic DropDownList - how to create one

From Dev

How to create a dynamic database connection in spring mvc application at runtime?

From Dev

How to create dynamic controls based on value in database using mvc 4

From Dev

Call extension method on derived type

From Dev

Generic type vs Extension method

From Dev

Extension method for nested generic type

Related Related

  1. 1

    How to create an extension method on IHtmlHelper<dynamic>

  2. 2

    How to create extension method for Enum type argument?

  3. 3

    How to create extension method for Enum type argument?

  4. 4

    How to create an extension method in TypeScript for 'Date' data type

  5. 5

    How To Create ToJsonAsync Extension Method?

  6. 6

    How to create an extension method for any type of Enum array in VB.Net

  7. 7

    How to work with Generic type members in extension method?

  8. 8

    How to create a linq extension method with Func<>?

  9. 9

    How to create the IQueryable version of IEnumerable extension method?

  10. 10

    how to create dynamic radio buttons in mvc razor

  11. 11

    How to use dynamic Type Hints in Laravel method

  12. 12

    How to create a delegate with a dynamic parameter type?

  13. 13

    Extension Method On Casted Type

  14. 14

    Extension Method with Generic Type

  15. 15

    Call method of dynamic type

  16. 16

    Extension method call not working in MVC

  17. 17

    How to Write Generic Extension Method to Convert Type in C#

  18. 18

    How to write an extension method to return the type being used?

  19. 19

    How can I get the generic type parameter of the 'this' parameter in an extension method?

  20. 20

    How can I get the generic type parameter of the 'this' parameter in an extension method?

  21. 21

    How to write an extension method to return the type being used?

  22. 22

    How to create a generic Task.ContinueWith extension method

  23. 23

    How to create an HtmlHelper extension method that will bind an IEnumerable<T> to a table

  24. 24

    ASP.Net MVC Dynamic DropDownList - how to create one

  25. 25

    How to create a dynamic database connection in spring mvc application at runtime?

  26. 26

    How to create dynamic controls based on value in database using mvc 4

  27. 27

    Call extension method on derived type

  28. 28

    Generic type vs Extension method

  29. 29

    Extension method for nested generic type

HotTag

Archive