Dropdownlist get's lost after invalid form submit

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

    Posted 29 Apr 2016 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
    3747 posts

    Posted 04 May 2016 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. Kay
    Kay avatar
    10 posts
    Member since:
    Apr 2016

    Posted 04 May 2016 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?

  4. Answer
    Georgi Krustev
    Admin
    Georgi Krustev avatar
    3747 posts

    Posted 09 May 2016 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!
     
  5. Mark White
    Mark White avatar
    6 posts
    Member since:
    Nov 2009

    Posted 31 Mar Link to this post

    Sorry to necro this thread, but I am having a very very similar issue.

    And while ModelState.Clear() IS the correct answer for this issue normally, then kendo dropdownlist FAILS to re-select the value after this code is done.

    Is there any more information on this issue? Specifically I set the Model.ItemID = 13 in the code, and then in the html, this is NOT selected.

  6. Mark White
    Mark White avatar
    6 posts
    Member since:
    Nov 2009

    Posted 31 Mar in reply to Mark White Link to this post

    I would also like to add that I tried to do this as well:
    ModelState.SetModelValue("ItemID ", new ValueProviderResult(param.ItemID, "", CultureInfo.InvariantCulture));
    But it has the same issue. The dropdownlist does not select the value that is set.
  7. Aleksandar
    Admin
    Aleksandar avatar
    205 posts

    Posted 03 Apr Link to this post

    Hi Mark,

    Did you try removing the "input-validation-error" class as suggested in the documentation? Also, I would advise updating to the latest Kendo UI version as it fixed an issue where input wrapped in class - 'input-validation-error' is not submitted when jquery-validate with version greater than version 1.12.0 is used in the application.

    Regards,
    Aleksandar
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  8. Mark White
    Mark White avatar
    6 posts
    Member since:
    Nov 2009

    Posted 24 Apr in reply to Aleksandar Link to this post

    Figured it out, it was actually a separate issue. There were multiple spaces coming out of the proc in the database, and the list was silently failing.

    It would be a good idea to put an error message in the console with something along the lines of "Selected item does not exist in the data structure". There seems to be a general lack of debugging messages that pop up when things go wrong.
  9. Christine
    Christine avatar
    6 posts
    Member since:
    Jul 2013

    Posted 23 Sep in reply to Aleksandar Link to this post

    I am using the latest version of the asp.net core library and I'm having the exact same problem with the drop down list control.  I thought this bug was supposed to be fixed?
  10. Ivan Danchev
    Admin
    Ivan Danchev avatar
    2105 posts

    Posted 28 Sep Link to this post

    Christine,

    Did you verify the DropDownList has the "input-validation-error" class applied to its wrapper element? If it does, have you tried removing it as suggested earlier in this thread?

    <script type="text/javascript">
        $(document).ready(function() {
            $(".k-widget").removeClass("input-validation-error");
        });
    </script>

    Regards,
    Ivan Danchev
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Back to Top