Set a default value if the bound value is null

2 posts, 0 answers
  1. Ramoji
    Ramoji avatar
    3 posts
    Member since:
    Nov 2012

    Posted 18 Jan 2014 Link to this post

    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.
                        .ComboBoxFor(model => model.SendFromId)
                        .HtmlAttributes(new { style = "width:250px", data_value_primitive = "true" })
                        .DataSource(source =>
                            source.Read(read =>
                                read.Action("People", "Home");
    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.

  2. Alexander Popov
    Alexander Popov avatar
    1411 posts

    Posted 22 Jan 2014 Link to this post

    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

    Alexander Popov
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET AJAX banner
Back to Top