Dropdownlist get's lost after invalid form submit

4 posts, 1 answers
  1. Kay
    Kay avatar
    5 posts
    Member since:
    Apr 2016

    Posted 29 Apr Link to this post

    I am using following code alongside multiple other inputs:

     

         @(Html.Kendo().DropDownListFor(m => m.CountryId)
                                  .HtmlAttributes(new { id = "CountryId" })
                                  .OptionLabel("Select country")
                                  .DataTextField("CountryName")
                                  .DataValueField("CountryId")
                                  .BindTo(UnityConfig.GetConfiguredContainer().Resolve<ISlViewModelBaseData>().GetCountries(SlLanguage.DefaultLanguageId))
                                  .Enable(true)
                                )

        @Html.ValidationMessageFor(m => m.CountryId)

    Problem:

    After sending the form without selecting any value from the list the dropdown box is bound, the form validation prevents from further action indicating an input error. That's fine.

    But after selecting a value and resending the form, the dropdown value is set back to OptionLabel "Select country".

    Inspecting with Fiddler reveals that the submitted form did not contain the field CountryId anymore.

    Sending the form directly with a selected value works fine, thogh.

    I assumed, that something on the post back after the sent invalid form went wrong but the response seems to be just fine.

    Could this be a bug?

     

    kind regards

    Kay

  2. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 04 May Link to this post

    Hello Kay,

    In general, the MVC DropDownList extension will retrieve the value from the ModelState (if any) on post back when the same view is returned (PRG approach is used). That being said, the only reason I can think of the widget to not select the declaratively defined value is that built-in functionality.

    Could you verify that the ModelState does not have any values that will interfere the behavior of the widget?

    If the problem still persists, I would ask you to send us a repro demo. Thus we will be able to review the case and follow you up with more details.

    Regards,
    Georgi Krustev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
  4. Kay
    Kay avatar
    5 posts
    Member since:
    Apr 2016

    Posted 04 May in reply to Georgi Krustev Link to this post

    Hi,

    thanks for your answer.

    I do not really understand how the linked article applies to my situation. Actually I do want a validated form after posting an invalid form:

    "ASP.NET MVC assumes that if you’re rendering a View in response to an HTTP POST, and you’re using the Html Helpers, then you are most likely to be redisplaying a form that has failed validation."

    -> That's exactly what I am doing.

    After sending the form with the default dropdown list value the Modelstate would contain :

    -        [18]    {System.Web.Mvc.ModelState}    System.Web.Mvc.ModelState
    +        Errors    Count = 1    System.Web.Mvc.ModelErrorCollection
        +        [0]    {System.Web.Mvc.ModelError}    System.Web.Mvc.ModelError
        +        Raw View        

    +        Value    {System.Web.Mvc.ValueProviderResult}    System.Web.Mvc.ValueProviderResult
    +        _errors    Count = 1    System.Web.Mvc.ModelErrorCollection

     

    In fact, Modelstate.Clear(); solves the problem but this of course also prevents validiation messages.

    I understand that it is the value of the model state dictionary that is beeing used during post-back (=> previous invalid value) which causes the dropdown list somehow to fail?

  5. Answer
    Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 09 May Link to this post

    Hello Kay,

    The shared links were just informative, to shed more light on how the ASP.NET MVC framework works.

    If the ModelState.clear() solves the issue, then the value that is coming from the server cannot be selected by the DropDownList for some reason. Could you verify that Model.CountryId contains the value it should?

    Another plausible cause for such erroneous behavior is an intervene of the jQuery.Validate framework:
    Try the suggestion from the last link. I am pretty sure that this is the cause of the problem.

    Regards,
    Georgi Krustev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready