locked
Order by Enum Name RRS feed

  • Question

  • User-240513752 posted

    I have a property of Enum in Model

      [Display(Name = "Phone Type")]
      public PhoneTypes PhoneTypeId { get; set; }
    

    Somewhere in project it is as

      public enum PhoneTypes
        {
            [Description("Not Set")]
            Notset = 0,
            [Description("Cell")]
            Cell = 1,
            [Description("Home")]
            Home = 2,
            [Description("Office")]
            Office = 3
        }

    I want to take Office first later what ever their sequence would be

    In view

    <div class="form-group form-group-sm">
                        <label class="control-label col-md-4">Phone Type</label>
                        <div class="col-md-8">
                        </div>
                        <div class="col-md-8">
                            @Html.EnumDescRadioListFor(model => model.PhoneTypeId, new
                       {
                           @id = "PhoneType",
                           @class = "form-control ddlInActive",
                           data_fv_regexp = "true",
                           data_fv_regexp_regexp = Constants.ValidNotSpecialSpaceRexBoot,
                           data_fv_regexp_message = RainMaker.Service.CommonService.GetResourceByName(Constants.ValidSelectList),
    
                       }, RadioButtonListType.Horizontal)
                            @Html.ValidationMessageFor(model => model.PhoneTypeId)
                        </div>
    
                    </div>

    How Enum is displaying

        public static HtmlString EnumDescRadioListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> modelExpression, object htmlAttributes, RadioButtonListType listType = RadioButtonListType.Vertical)
            {
                var b = GetEnumValueList(modelExpression);
    
                //var values = from enumT e in Enum.GetValues(typeOfProperty)
                //             where GetEnumDescription<enumT>(e) != null
                //             select new { Id = e, Name = GetEnumDescription<enumT>(e) };
    
                return htmlHelper.RadioEnumListFor(modelExpression, new SelectList(b, "Id", "Name"), htmlAttributes, listType, GetEnumSelectedValue(modelExpression, htmlHelper));
            }

    Wednesday, December 28, 2016 1:02 PM

All replies

  • User-691209617 posted

    use this 

    var values = (from enumT e in Enum.GetValues(typeOfProperty)
                             where GetEnumDescription<enumT>(e) != null
                             select new { Id = e, Name = GetEnumDescription<enumT>(e) }).OrderByDescending(x => x.Name);

    Wednesday, December 28, 2016 1:17 PM
  • User-240513752 posted

    use this 

    var values = (from enumT e in Enum.GetValues(typeOfProperty)
                             where GetEnumDescription<enumT>(e) != null
                             select new { Id = e, Name = GetEnumDescription<enumT>(e) }).OrderByDescending(x => x.Name);

    this is giving me build error .. check the image here

    https://postimg.org/image/awm7hk2l5/

    Thursday, December 29, 2016 5:26 AM
  • User-707554951 posted

    Hi khan_1,

    From you error message.

    Are you  add namespace of you model to your controller?

    Besides, why you copy codemovement’s code directly without doing any changes?

    enumT shoul be replace with phoneTypes.

    then, you could move you mouse on GetEnumDescription()  method to see potential fixs

    Best Regards

    Cathy

    Thursday, December 29, 2016 8:14 AM