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

Initial value of list not bound

2 Answers 157 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
AP
Top achievements
Rank 1
Iron
Iron
Veteran
AP asked on 25 Jan 2013, 01:28 PM
I've got several dropdown lists in an editor template, for use with a grids pop-up edit window.

They are displayed fine, and if a value other than the default one is selected, data is returned to the controller.

However, if the user leaves the default values as presented, null values are returned to the controller.

The definition is:
@(Html.Kendo().DropDownListFor(c=>c.Site)
.Name("Site")
.DataTextField("SiteDescription")
.DataValueField("Site")
 
 
.DataSource(source=>source
    .Read(read=>read.Action("GetSiteLookUpList","ManualData"))))
@Html.ValidationMessageFor(c => c.Site  )
Do I need to set anything else so the default value is posted back?



2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 29 Jan 2013, 08:47 AM
Hello,

A value will not be set if the user never selects a value if the value in the model does not match any of the values loaded in the dropdown. You could use one of the following approaches to avoid this case:

  1. Use an OptionLabel for the dropdown so that it is selected by default and the user is forced to select a value.
  2. Use the dropdownlist DataBound event to set the value to the Grid model to be equal to the first item in the dropdown.
  3. If the value is not matched only for new records, you could also set a default value through the Grid DataSource Model.
Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
AP
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 29 Jan 2013, 10:34 AM
Thanks for your reply.

It's not very intuitive, as the in the insert form, the initial values are displayed, even though they won't be posted.

I've added a select option, as hard-coding default values isn't a good idea, as these are table based lookups, which frequently get changed.

Tags
DropDownList
Asked by
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
Daniel
Telerik team
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Share this question
or