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
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
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?
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
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.
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.
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
Our thoughts here at Progress are with those affected by the outbreak.
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.
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/.