This is a migrated thread and some comments may be shown as answers.

OptionLabel

6 Answers 457 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Dina
Top achievements
Rank 1
Dina asked on 30 Apr 2017, 01:55 AM

Why dropdown with bind to Enum and OptionLabel when initialized is set to first value of enum and not to OptionLabel

@Html.Kendo().DropDownListFor(model => model.Education).OptionLabel("Please choose...").BindTo(Html.GetEnumSelectList<MyProject.Models.Enums.Education>()).HtmlAttributes(new { style = "width:100%;" })

 

Here I have dropdown with option label as first row and Enum displaynames as following but control is set to first item from enum list.

In my case I set [Required] attribute for Education in module and I expect control stays at "Please choose..." and validation is not allow to save form without choosing value. But I get control set to first value so user can simply ignore this control

6 Answers, 1 is accepted

Sort by
0
Veselin Tsvetanov
Telerik team
answered on 03 May 2017, 10:36 AM
Hi Dima,

The reason for the observed is the fact that the default value for int is "0". When you create an enum in C# in the following way:
public enum Education
{
    Trivial,
    Regular,
    Important,
    Critical
}

the values in the Enumeration are automatically assigned. This means, that the "Trivial" option, being on the index 0, corresponds to the number 0. Therefore, when you pass a model with no ​Education property set, it defaults to 0 and forces the DropDownList to display the first option (the one at index 0) as selected.

To change the above behaviour, you could alter the declaration of the enum:
public enum Education
{
    Trivial = 1,
    Regular = 2,
    Important = 3,
    Critical = 4
}

In this way the 0 won't be a valid selection option and the DropDownList will display its Option label, when the model does not have the Education property set.

Attached you will find a simple project, implementing the above suggestion.

Regards,
Veselin Tsvetanov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Jonathan
Top achievements
Rank 2
answered on 26 Oct 2017, 10:48 PM

Hi Veselin, I have the same problem - have applied fixes as suggested above but to no avail. I am using MVC 5.3 with Kendo version 2017 v3.

The enum dropdown is showing no option label but an empty value in the first position (representing the default int value - zero which does not exist in the enum!), all other values appear as expected. WIth standard MVC enum helpers the display work fine without this issue but I want to use yours because because of consistent styling. What is going wrong here, bug or configuration?

Would appreciate some clues

@(Html.Kendo().DropDownListFor(m => m.OwnerModal)
                       .DataTextField("Text")
                       .DataValueField("Value")
                       .OptionLabel(Resources.PleaseSelect)
                       .BindTo(EnumHelper.GetSelectList(Model.OwnerModal.GetType(), Model.OwnerModal))
                       .Events(e =>
                       {
                           e.Select("ApprovalProcessController.selectOwnerModal");
                       })
                       )

 

 public enum ApprovalOwnerType
    {
        
        [System.ComponentModel.DataAnnotations.DisplayAttribute(Name="ApprovalOwner_Party", ResourceType = typeof(Resources.Resources))]
        Party = 1,
        
        [System.ComponentModel.DataAnnotations.DisplayAttribute(Name="ApprovalOwner_Contract", ResourceType = typeof(Resources.Resources))]
        Contract = 2,
        
        [System.ComponentModel.DataAnnotations.DisplayAttribute(Name="ApprovalOwner_Audit", ResourceType = typeof(Resources.Resources))]
        Audit = 3,
        
        [System.ComponentModel.DataAnnotations.DisplayAttribute(Name="ApprovalOwner_Complaint", ResourceType = typeof(Resources.Resources))]
        Complaint = 4,
        
        [System.ComponentModel.DataAnnotations.DisplayAttribute(Name="ApprovalOwner_Financial", ResourceType = typeof(Resources.Resources))]
        Financial = 5,
        
        [System.ComponentModel.DataAnnotations.DisplayAttribute(Name="ApprovalOwner_Person", ResourceType = typeof(Resources.Resources))]
        Person = 6,
    }
}

 

//viewmodel field...nothing fancy... no constructor...

 [Display(Name = "ApprovalOwner", ResourceType = typeof(Resources.Resources))]
            public ApprovalOwnerType OwnerModal { get; set; }

 

 

0
Veselin Tsvetanov
Telerik team
answered on 31 Oct 2017, 10:09 AM
Hello Jonathan,

Attached you will find a modified version of the sample already discussed, based on the snippets sent. You will notice, that by default the Option label is set for the DropDownList HTML helper. May I ask you to modify this sample, so it reproduces the problem observed at your end and send it back to us?

Regards,
Veselin Tsvetanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Jonathan
Top achievements
Rank 2
answered on 01 Nov 2017, 04:38 PM

Hi Veselin,

Thank you for the sample project which I note was a dot net core app not MVC5.3 however that would appear irrelevant. My production code appears to now work as expected and on re-testing in VS2015 using a local IIS, I cannot get the condition I mentioned to now occur. The drop-down correctly populates with no zero index entry. I did at the time clear the browser cache, as I always do, when retesting but perhaps the local IIS instance held the previous code somehow as it now works correctly having rebooted the next day and having restarted VS2015 (as it always should have done!). Apologies and please close the issue.

0
Jonathan
Top achievements
Rank 2
answered on 10 Nov 2017, 06:41 PM

Veselin, apologies for re-opening the ticket, for your information (and others) I have discovered when the error occurs,

If one uses "DropDownListFor" as my per my initial example above then the error occurs, a blank entry is created as the first list item.

Using the following i.e. "DropDownList", it works as expected:

@(Html.Kendo().DropDownList()
.Name("OwnerModal")
.DataTextField("Text")
.DataValueField("Value")
.OptionLabel(Resources.PleaseSelect)
.BindTo(EnumHelper.GetSelectList(Model.OwnerModal.GetType(), Model.OwnerModal))
)

 

Hope this helps somebody

J

 

0
Veselin Tsvetanov
Telerik team
answered on 15 Nov 2017, 09:34 AM
Hello Jonathan,

Thank you for the additional details provided.

Attached you will find an MVC 5.2.3 .NET project (the latest released version of MVC 5), which implements the described scenario. You will notice, that the option label text displays properly in the drop-down. May I ask you to specify how the attached should be modified, so issue could be observed?

Regards,
Veselin Tsvetanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Dina
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Jonathan
Top achievements
Rank 2
Share this question
or