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

Set a default value if the bound value is null

1 Answer 795 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Ramoji
Top achievements
Rank 1
Ramoji asked on 18 Jan 2014, 10:53 AM
Hi ,

I am using a combobox with  a remote datasource. This field is bound to an integer property on my model.In the case of adding creating a new record combobox doesn't show any selection as the property is null.
@{
                Html.Kendo()
                    .ComboBoxFor(model => model.SendFromId)
                    .DataTextField("Text")
                    .DataValueField("Value")
                    .HtmlAttributes(new { style = "width:250px", data_value_primitive = "true" })
                    .DataSource(source =>
                    {
                        source.Read(read =>
                        {
                            read.Action("People", "Home");
                        });
                    })
                    .Events(e=>
                            {
                                e.Change("onComboChanged");
                            })
                    .Deferred()
                    .Render();
            }
Now  I need to set a default value when the property is null. I have tried to set using the  Value but it causes problem while edit.Though I change the selection and submit the form the Value parameter shows up again if I try to edit the record.
Did anyone have this kind of scenario before. Any help will be much appreciated.

Thanks,
Ramoji

1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 22 Jan 2014, 07:37 AM
Hi Ramoji,

Basically there are two possible approaches:
  • Add a default value to the Model field  
  • Use the ComboBox's dataBound event to check the current value. If it's empty - set it to the desired default value

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
ComboBox
Asked by
Ramoji
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or