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

Dropdownlist get's lost after invalid form submit

9 Answers 1311 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Kay
Top achievements
Rank 1
Kay asked on 29 Apr 2016, 09:21 AM

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

9 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 04 May 2016, 07:11 AM
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!
 
0
Kay
Top achievements
Rank 1
answered on 04 May 2016, 08:42 AM

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?

0
Accepted
Georgi Krustev
Telerik team
answered on 09 May 2016, 07:36 AM
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!
 
0
Mark White
Top achievements
Rank 1
answered on 31 Mar 2020, 05:31 PM

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.

0
Mark White
Top achievements
Rank 1
answered on 31 Mar 2020, 06:08 PM
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.
0
Aleksandar
Telerik team
answered on 03 Apr 2020, 01:05 PM

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.
0
Mark White
Top achievements
Rank 1
answered on 24 Apr 2020, 01:51 PM
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.
0
Christine
Top achievements
Rank 1
Veteran
answered on 24 Sep 2020, 04:45 AM
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?
0
Ivan Danchev
Telerik team
answered on 28 Sep 2020, 04:41 PM

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/.

Tags
DropDownList
Asked by
Kay
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Kay
Top achievements
Rank 1
Mark White
Top achievements
Rank 1
Aleksandar
Telerik team
Christine
Top achievements
Rank 1
Veteran
Ivan Danchev
Telerik team
Share this question
or