Unplanned
Last Updated: 27 Jan 2021 12:58 by ADMIN
Marc
Created on: 26 Jan 2021 22:37
Category: UI for ASP.NET MVC
Type: Bug Report
0
ViewBag values being set as form element default values

Hi there,

We've just hit a strange issue.  We have a DropDownList called "Title" that displays "Mr", "Mrs", "Ms" etc.  However, we sometimes noticed that the Title submitted to the form was "LoanBorrower".  This was strange as the only place "LoanBorrower" existed was in the MVC back end of the page with "ViewBag.Title = "LoanBorrower".

It appears that if the ViewBag has an entry with the same name as a Kendo form element, the elements default value is being set as the value in the ViewBag.

I have tried this with ViewBag entries other than Title, and the result is the same.

 

To Reproduce

At the top of my "Borrower.cshtml" page I have the following:

@{
    ViewBag.Title = "LoanBorrower";
}

 

Kendo DropDownList

I stripped the DropDownList down to the bare minimum and still had the problem:

@Html.Kendo().DropDownListFor(m => m.Title)

 

The control rendered as the following.  Note the value is set to "LoanBorrower" which is the value in ViewBag.Title:

<input data-val="true" data-val-length="Title must be between 1 and 20 characters" data-val-length-max="20" data-val-length-min="1" data-val-required="Select a title." id="Title" name="Title" type="text" value="LoanBorrower" />
<script>
	kendo.syncReady(function(){jQuery("#Title").kendoDropDownList({});});
</script>

 

Kendo TextBox

I then changed the Kendo control to a Kendo TextBox and had the same problem:


@Html.Kendo().TextBoxFor(m => m.Title)

 

The control rendered as the following.  Again, the value is set to "LoanBorrower":

<input data-val="true" data-val-length="Title must be between 1 and 20 characters" data-val-length-max="20" data-val-length-min="1" data-val-required="Select a title." id="Title" name="Title" value="LoanBorrower" />
<script>
	kendo.syncReady(function(){jQuery("#Title").kendoTextBox({});});
</script>

 

Standard TextBox

I then changed the control to a standard HTML Textbox and no longer had the issue.

@Html.TextBoxFor(m => m.Title)

 

The control rendered as the following.  Note that this time, the value is an empty string which is correct.

<input data-val="true" data-val-length="Title must be between 1 and 20 characters" data-val-length-max="20" data-val-length-min="1" data-val-required="Select a title." id="Title" name="Title" type="text" value="" />

 

 

 

 

 

 

 

 

1 comment
ADMIN
Ianko
Posted on: 27 Jan 2021 12:58

Hello Marc,

Thank you for this report. I converted the item to a bug report. 

A possible workaround is to explicitly set up the Value method to the model's field like so:

@(Html.Kendo().DropDownListFor(m => m.Title)
        .Value(Model == null ? "" : Model.Title)
)

Regards,
Ianko
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/.