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

Custom Template DropDownList: Initial value not set on save

13 Answers 359 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel Knoll
Top achievements
Rank 1
Daniel Knoll asked on 01 Feb 2013, 10:11 AM
Hi,

I'm using the Kendo UI ASP.NET MVC Grid with a Custom Template which includes a DropDownList in a column. The data must be loaded based on different filters, so I populate the DropDownList via a DataSource.

Everything works fine, except if I do not change the value.
These are the steps:
* The grid is loaded, there is no option label, so the first entry is visible.
* If I change the value in the DropDownList, everything works fine and the value is sent to the Server
* If I do not change the value, I get null as value on the server.

It seems like the initial value is not sent back to the server.

What can I do about this? My Kendo Version is 2012.3.1114.

Greets

Attached my custom template in the grid:
@(Html.Kendo().DropDownListFor(m => m)
    .Name("SalesRep")
    .DataSource(
        config => config.Read(read => read.Action("SalesRepRead", "TemplateData", new { area = "" }).Data("filterGrid")))
    .DataValueField("Value")
    .DataTextField("Text")   
)

13 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 05 Feb 2013, 09:59 AM
Hello,

This behavior is expected if the value in the Grid is not matched in the items that are loaded. In order to avoid showing an item which value is not set to the model, you could use one of the following options:

  • Use an OptionLabel.
  • Bind a one time handler to the dropdownlist databound event and set the value in the model to be the same as in the dropdown.
  • If the value is matched only for new items, you can also set a default value through the Grid DataSource Model.
Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Kelso
Top achievements
Rank 1
answered on 02 Mar 2015, 05:02 PM
The answer here is not entirely correct, even if the default value does match an Id for that drop down, it will still not send the value to the controller without using optionLabel
0
Daniel
Telerik team
answered on 04 Mar 2015, 11:00 AM
Hello Kelso,

Could you provide a runnable sample that demonstrates the issue? The default value was sent correctly without using option label at least when I tested the scenario on my side.

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kelso
Top achievements
Rank 1
answered on 04 Mar 2015, 04:13 PM
Here are screenshots of the various items to setup the dropdowns for my inline edit. I am using the Columns.ForeignKey() and not setting a defaultvalue(). I really should not have to since the first value is already selected, and it should be able to pick it up.
0
Daniel
Telerik team
answered on 06 Mar 2015, 09:56 AM
Hello again Kelso,

As far as I understood from your previous post, the problem was in case a default value that matches an item from the dropdownlist is set. If the problem is in the scenario a default value is not set then please check this issue for more detailed information about the problem.

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kelso
Top achievements
Rank 1
answered on 06 Mar 2015, 02:09 PM
Let me clarify, since your answer has no relevance to the issue at hand.

1) MVVM is not being used.
2) The issue is, if the kendo dropdown is being used with the GridForeignKey column, regardless of the datasource, if no change event is fired for the dropdown, it will not send the value of the item showing in the dropdown to the controller. I don't think I can make it any more clear than that.
3) This happens even if the items DO MATCH.
4)The corrective action on the part of telerik is the make sure the value from the dropdown is populated to the viewmodel that is passed back to the controller, even if the change event for the dropdown is never fired. 

The screenshots I sent clearly show the code to reproduce the issue with all default telerik templates.
0
Daniel
Telerik team
answered on 10 Mar 2015, 10:01 AM
Hello,

The grid editor uses the MVVM bindings so the scenario is the same as the one discussed in the issue that I linked in my previous reply. If the value is not sent even if the value in the model is the same as the selected item value then I will again ask for a runnable sample so we can investigate further.

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kelso
Top achievements
Rank 1
answered on 11 Mar 2015, 05:19 PM
I know people get busy, but this project took me all by 20 mins (if that) to create and write. Just create a new telerik MVC project and replace the home controller, and index.cshtml file in the views/home directory, and add the testmodel.cs to the models directory. I would have included the entire project but the telerik mvc project is so bloated its 29 megs zipped.  Run the project, click edit or create and click the update button without doing anything else, and have a look at the output window in Visual studio and you will see the values being passed as 0 or empty string/null
0
Daniel
Telerik team
answered on 13 Mar 2015, 10:41 AM
Hello,

The value in the model is not the same as the selected item value with the code that you provided. The default value for the SelectedValue property is 0 and the value of the first item in the "TestList" is 1.

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kelso
Top achievements
Rank 1
answered on 13 Mar 2015, 11:19 AM
That is EXACTLY my point, I am not setting a default value on the front end the value selected is the one that is returned in the foreignkey column set from the server, it SHOULD maintain the value of the currently selected item that is in the drop down without needing to fire the change event, did you bother to change the value of the loop so both start at 1?? The only thing that changes is the update will not fire because no changes to the record have been made, but it will still returns 0 value and no data on the create if you do not actually change the dropdown selection. Please stop wasting my time doing your job for you, you would have clearly been able to see this if you had bothered to put just a small amount of effort into this.
0
Kelso
Top achievements
Rank 1
answered on 13 Mar 2015, 12:36 PM
I even went further to test, and I added a string field to the model to verify the update method, update method works as expected, Create however passes no value if the value in the dropdown is not changed from what is loaded, if you only change the text value of the string field, and leave whatever value was loaded into the SelectedValue property from the server it will pass 0 to the controller in the viewmodel.
0
Daniel
Telerik team
answered on 17 Mar 2015, 09:02 AM
Hello,

I am not sure what else can I do to help with the the case when the value is different. As mentioned in my initial post, the behavior is currently expected and I have listed the possible approaches that  can be used to avoid the problem. Also, as my colleague said in the linked issue the behavior is already considered to be changed. 

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kelso
Top achievements
Rank 1
answered on 17 Mar 2015, 01:52 PM
I don't think you understand what I am saying, I am not saying I cannot get around it, I am saying the behavior itself is wrong and should be corrected. If there is a selected value, regardless of a change event being fired, it should accept the value that is displayed in the dropdown. The way around it is to force the user to make a change, which is what happens when you edit the template to have a default value. The improvement is for it to pass whatever the initial value is in the foreignkey column back to the controller method. (If it never gets changed) The value is there, you can access it on the client like #= ColumnName # and this would be fine for the developer to do in a simple scenario, where your going to have problems is in nested templates, where using #= # causes an invalid template error. Which is the entire point I was making in the first place.
Tags
Grid
Asked by
Daniel Knoll
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Kelso
Top achievements
Rank 1
Share this question
or