[RESOLVED]Using extension method with dropdownlist

I’m looking for a way to bind extension method in a dropdownlist (in a Post form)

The extension was made fro enum list for a friendly name .

My complete  code :

public enum MyDayOfWeek
    {
        Sunday = 0,
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
        SunTilFir = 7,
        SunTilSat=8,
        SunTilTHur=9,

    }


    public class Course
    {

        public int CourseId { get; set; }
        public string CourseName { get; set; }
        public int Hour { get; set; }
        public MyDayOfWeek Days { get; set; }
        public string DaysFriendlyName
        {
            get
            {
                return this.Days.ToFriendlyName();
            }
        }
    }
    public static class DaysEnumExtensions
    {
        public static string ToFriendlyName(this MyDayOfWeek days)
        {
            switch (days)
            {
                case MyDayOfWeek.SunTilFir:
                    return "Sunday until friday";
                case MyDayOfWeek.Sunday:
                    return "Sunday";
                case MyDayOfWeek.Monday:
                    return "Monday";
                case MyDayOfWeek.Tuesday:
                    return"Tuesday";
                case MyDayOfWeek.Wednesday:
                    return"Wednesday";
                case MyDayOfWeek.Thursday:
                    return"Thursday";
                case MyDayOfWeek.Friday:
                    return"Friday";
                case MyDayOfWeek.Saturday:
                    return"Saturday";
                case MyDayOfWeek.SunTilSat:
                    return"Sunday until Satrday";
                case MyDayOfWeek.SunTilTHur:
                    return" Sunday until Thursday";

            }
            return "Oh Oh Horrible Failure!!";
        }
    }

The view :

@model QDeleteMe.Models.Course  
    @using QDeleteMe.Models
//I have remove non relevant items
 <div class="form-group">
            @Html.LabelFor(model => model.DaysFriendlyName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EnumDropDownListFor(model => model.Days.ToFriendlyName())
                @Html.ValidationMessageFor(model => model.Days, "", new { @class = "text-danger" })
            </div>
        </div>

The Error :
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Thanks.

try this

                <select id="Days" name="Days">
                    
                    @{

    var days = Enum.GetValues(typeof(MyDayOfWeek));

    foreach (var d in days)
    {
        string fn = DaysEnumExtensions.ToFriendlyName((MyDayOfWeek)d);
                    <option value="@d">@fn</option>
    }

    }
                </select>

                @Html.ValidationMessageFor(model => model.Days, "", new { @class = "text-danger" })

public static class DaysEnumExtensions
    {
        public static string ToFriendlyName(MyDayOfWeek days)
        {
            switch (days)
            {
                case MyDayOfWeek.SunTilFir:
                    return "Sunday
---
 public class Course
    {

        public int CourseId { get; set; }
        public string CourseName { get; set; }
        public int Hour { get; set; }
        public MyDayOfWeek Days { get; set; }
        public string DaysFriendlyName
        {
            get
            {
                return DaysEnumExtensions.ToFriendlyName(this.Days);
            }
        }
    }

loading with value and text

 <div class="form-group">
            @Html.LabelFor(model => model.DaysFriendlyName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <select id="Days" name="Days">
                    
                    @{

    var days = Enum.GetValues(typeof(MyDayOfWeek));

    foreach (var d in days)
    {
        string fn = DaysEnumExtensions.ToFriendlyName((MyDayOfWeek)d);
        int n = (int)((MyDayOfWeek)d);
        <option value="@n">@fn</option>
    }

                    }
                </select>

                @Html.ValidationMessageFor(model => model.Days, "", new { @class = "text-danger" })
            </div>
        </div>

cnuonline

loading with value and text

 <div class="form-group">
            @Html.LabelFor(model => model.DaysFriendlyName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <select id="Days" name="Days">
                    
                    @{

    var days = Enum.GetValues(typeof(MyDayOfWeek));

    foreach (var d in days)
    {
        string fn = DaysEnumExtensions.ToFriendlyName((MyDayOfWeek)d);
        int n = (int)((MyDayOfWeek)d);
        <option value="@n">@fn</option>
    }

                    }
                </select>

                @Html.ValidationMessageFor(model => model.Days, "", new { @class = "text-danger" })
            </div>
        </div>

excellent . thank you.

Leave a Reply