0

I'm having difficulties with a telerik combobox that displays a blank value instead of the model's current value.

I have the below model :

    public decimal? ResourceId { get; set; }      
    public string ResourceName{ get; set; }        
    public decimal ResourceTypeId { get; set; }      
    public string ResourceTypeName{ get; set; }        

I wanted my view to display the ResourceTypeName in a combobox in order to be able to change it so I added the below to my model

    public IEnumerable<ResourceTypeModel> ResourceType()
    {
        ResourceTypeModel a = new ResourceTypeModel();

        a.ResourceTypeId = this.ResourceTypeId ;
        a.ResourceTypeName= this.ResourceTypeName;

        List<ResourceTypeModel> MyList= new List<ResourceTypeModel>();
        MyList.Add(a);

        return MyList;

    }

Now, my view contains the following combobox :

        @(Html.Telerik().ComboBoxFor(model => model.ResourceTypeId)
        .AutoFill(true)
        .DataBinding(binding => binding.Ajax().Select("_AutoCompleteAjaxLoadingResourceTypeList","ResourceController"))
        .BindTo(new SelectList(Model.ResourceType(), "ResourceTypeId", "ResourceTypeName"))                    
        .Filterable(filtering =>
        {
            filtering.FilterMode(AutoCompleteFilterMode.Contains);
            filtering.MinimumChars(2);
        })
        .HighlightFirstMatch(true)                    
        )

But when my view opens, the combobox shows a null value.

If I click the dropdown button of the combobox, the value I want to display appears as if it was a second item of the list and I can select it.

The combobox works perfectly for the autocompletion part and correctly shows all my available values when typing in it but simply does not show up directly with the needed value.

DonQi
  • 409
  • 2
  • 6
  • 10

1 Answers1

0

So, here is my solution, it now does what i want but I still do not understand why it was not working.

I modified my model and switched from a method returning the ResourceType list to an Ienumerable attribute as follows :

public IEnumerable<ResourceTypeModel> ResourceType{ get; set; }

I modified my model builder to feed that "mono-item list" with my ResourceType and modified the view to use it in the ".BindTo"

        @(Html.Telerik().ComboBoxFor(model => model.ResourceTypeId)
        .AutoFill(true)
        .DataBinding(binding => binding.Ajax().Select("_AutoCompleteAjaxLoadingResourceTypeList","ResourceController"))
        .BindTo(new SelectList(Model.ResourceType, "ResourceTypeId", "ResourceTypeName"))                    
        .Filterable(filtering =>
        {
            filtering.FilterMode(AutoCompleteFilterMode.Contains);
            filtering.MinimumChars(2);
        })
        .HighlightFirstMatch(true)                    
        )

I must be doing something wrong somewhere because I don't get why I need to modify the model and add a list in it to display the current value inside the combobox

DonQi
  • 409
  • 2
  • 6
  • 10