I use Combobox with Ajax Loading.
And I have a question:
1) I have a code:
@model int
@(Html.Kendo().ComboBoxFor(x => x)
.DataTextField("Description")
.DataValueField("Id")
.Placeholder("Select")
.HtmlAttributes(new { style = "width:225px", id = Guid.NewGuid().ToString() })
.DataSource(databinding => databinding
.Read(read => read
.Action("SelectComboboxItems", "Lumber")))
)
But If I use ComboboxFor...than I get this...(please see zero.png)
Why Combobox does this? I want that combobox set placeholder instead of 0, when I dont have item with Id=0
10 Answers, 1 is accepted
The described behavior is expected, because the ComboBox widget is designed to accept custom values. The default value of int is 0. When you create new item you will see the default value of the integer as ComboBox value. You can either make Model of the editor template Nullable<int> or to clear the value of the widget when Model is 0.
Georgi Krustev
Telerik
How I can do this: "Clear the value of the widget when Model is 0."
I can subscribe on Event DataBound and then clear value, but how I can do this in EditorTemplate?
I will suggest you try clear the default value of the integer using the Value method of the wrapper:
@model
int
@(Html.Kendo().ComboBoxFor(x => x)
.Value(Model == 0 ?
string
.Empty : Model.ToString())
.DataTextField(
"Description"
)
.DataValueField(
"Id"
)
.Placeholder(
"Select"
)
.HtmlAttributes(
new
{ style =
"width:225px"
, id = Guid.NewGuid().ToString() })
.DataSource(databinding => databinding
.Read(read => read
.Action(
"SelectComboboxItems"
,
"Lumber"
)))
)
Georgi Krustev
Telerik
I try this method earlier, but ".Value()" in wrapper Combobox doesn't work for me :(
If I use .Value(), then nothing changes.
If declaratively value setting does not work, then probably widget's value is set subsequently. In that case I will need more information about your current implementation in order to advice you further.
Georgi Krustev
Telerik
@model int
@(Html.Kendo().ComboBoxFor(x => x)
.Value(Model == 0 ? string.Empty : Model.ToString())
.DataTextField("Description")
.DataValueField("Id")
.Placeholder("Select")
.HtmlAttributes(new { style = "width:225px", id = Guid.NewGuid().ToString() })
.DataSource(databinding => databinding
.Read(read => read
.Action("SelectComboboxItems", "Lumber")))
)
[Required]
[UIHint("PlanLumberList")]
[Display(Name = "IdLumberDisplay", ResourceType = typeof(LocalizationLumber))]
public virtual int IdLumber { get; set; }
columns.Bound(o => o.IdLumber).ClientTemplate("#= LumberName #").Width(160);
In that case, you can wire the edit event of the grid. Thus you can get the combobox widget and clear the
default value:
function edit(e) {
var combobox = e.container.find("#PlanLumberList").data("kendoComboBox");
if ( combobox && combobox.value() === "0" ) {
combobox.value("");
}
}
Regards,
Georgi Krustev
Telerik
Thanks for help.
Hi Osvaldo,
Can you be more exact with your feedback about the ComboBox component? What functionality would you like to have when using it? What additional work and issues you have had when using the component?
This post is already seven years old. I don't know if your comment is related to the current thread but if you haven't worked with our latest product version, I would recommend trying it. Check the Combobox functionalities and let me know what is missing in the current implementation.
Regards,
Petar
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.