Initial value of list not bound

3 posts, 1 answers
  1. AP
    AP avatar
    187 posts
    Member since:
    Apr 2010

    Posted 25 Jan 2013 Link to this post

    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.ValidationMessageFor(c => c.Site  )
    Do I need to set anything else so the default value is posted back?

  2. Answer
    Daniel avatar
    2110 posts

    Posted 29 Jan 2013 Link to this post


    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.
    the Telerik team
    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
  4. AP
    AP avatar
    187 posts
    Member since:
    Apr 2010

    Posted 29 Jan 2013 Link to this post

    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.

Back to Top