DropDownListFor return value of [object Object]

12 posts, 0 answers
  1. Steven
    Steven avatar
    29 posts
    Member since:
    Jul 2012

    Posted 17 Apr 2013 Link to this post

    I have declared the following DropDownList

                @(Html.Kendo().DropDownListFor(model => model.CurrencyCode)
                    .OptionLabel("Select Currency Code...")
                    .Items(items => {
                        items.Add().Text("U.S. Dollars").Value("USD");
                        items.Add().Text("British Pounds").Value("GBP");
                        items.Add().Text("Hong Kong Dollars").Value("HKD");
                        items.Add().Text("Euros").Value("EUR");
                        items.Add().Text("Chinese Yuan").Value("RMB");
                        items.Add().Text("Russian Rubles").Value("RUB");
                    })
                )

    When an item is first selected, the value is set to [object Object].  However, after the record is saved, the second time it is selected, the value is set to the correct "Value".

    Any ideas what may be causing this?
  2. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 18 Apr 2013 Link to this post

    Hello Steven,

     
    You will need to define the DataTextField and DataValueField:

    @(Html.Kendo().DropDownListFor(model => model.CurrencyCode)
                    .OptionLabel("Select Currency Code...")
                    .DataTextField("Text")
                    .DataValueField("Value")
                    .Items(items => {
                        items.Add().Text("U.S. Dollars").Value("USD");
                        items.Add().Text("British Pounds").Value("GBP");
                        items.Add().Text("Hong Kong Dollars").Value("HKD");
                        items.Add().Text("Euros").Value("EUR");
                        items.Add().Text("Chinese Yuan").Value("RMB");
                        items.Add().Text("Russian Rubles").Value("RUB");
                    })
                )
    In the next internal build we will enhance current behavior and will set automatically DataTextField/DataValueField field to "Text" and "Value".

    Regards,
    Georgi Krustev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Steven
    Steven avatar
    29 posts
    Member since:
    Jul 2012

    Posted 07 May 2013 Link to this post

    Georgi,

    I tried your suggestion, but the behavior is still the same.  One thing I forgot to mention is that this dropdownlist is within the popup editor for a kendo grid.  I have tried to trace down where this would affect the behavior, but have been unable to do so.

    Thank you for your response.
  5. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 08 May 2013 Link to this post

    Hello again Steven,

     
    I suppose that the issue is related to the ability of the MVVM (used in grid's editing) to set a whole object to the model's property if its initial value is null. I will suggest you check this code library, which shows how to implement editing with dropdownlist widget.

    All the best,
    Georgi Krustev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Tom
    Tom avatar
    12 posts
    Member since:
    May 2013

    Posted 29 May 2013 Link to this post

    Georgi,

    I'm having the same problem.  Can you provide some additional information about using the 'code library' you referenced?  I've got it working with a List<string> but can't seem to get it working when passing a more complex object - even when setting the DataTextField and DataValueField.
  7. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 31 May 2013 Link to this post

    Hello Tom,

     
    Check the default value of the property, which dropdownlist modifies. In general, when initial value of the property is null, then the value binding will try to assign it the whole data item, not only the corresponding string value. That is why, you will need to define a default value different than null.

    Regards,
    Georgi Krustev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  8. Frederick
    Frederick avatar
    11 posts
    Member since:
    Jun 2013

    Posted 02 Jul 2013 Link to this post

    I understand what the demo project is doing here I think.  However, what about a scenario where you have two or more cascading drodownlists in the popup editor.  Also, is there an option other than using ViewData or ViewBag to retrieve the data for the BindTo attribute of the dropdown list?  Something more similar to  the ability to reference a controller action in the DataSource attribute of the dropdownlist?

    Think of the scenario of states and counties where state and county would be stored in database tables and would each get a model in the project.  We would need to be able to have cascading dropdownlists with the county controls items getting updated when the state is updated.  We wouldn't want to have to load all counties from the database and store them in ViewData as that would be rather wasteful.
  9. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 05 Jul 2013 Link to this post

    Hello Frederick,

     
    I will suggest you check this code library, which shows how to implement popup editing using cascading dropdownlists bound to a remote service. The other option of BindTo is to use Ajax binding shown in the code library.

    Regards,
    Georgi Krustev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  10. Robert
    Robert avatar
    3 posts
    Member since:
    Jul 2010

    Posted 06 Dec 2013 Link to this post

    "In general, when initial value of the property is null, then the value binding will try to assign it the whole data item, not only the corresponding string value. That is why, you will need to define a default value different than null."

    You have got to be kidding me...!

    And what about nullable properties in the model? 

    I've just wasted 3 hours wrestling with this bug trying to get a nullable Guid to be populated. So now I have to have a non-nullable Guid and check if that Guid is empty in my controller? That is sloppy code.
  11. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 09 Dec 2013 Link to this post

    Hello Robert,

    You can set the valuePrimitive option to true. Thus the widget will notify the Kendo binder to not perform the aforementioned behavior.

    Regards,
    Georgi Krustev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  12. Kevin F
    Kevin F avatar
    46 posts
    Member since:
    Sep 2009

    Posted 28 Jan Link to this post

    I'm going insane with this problem.  I have a normal kendoDropDownList with nothing special going on:

    01.availableReports = $("#ddlAvailableReports").kendoDropDownList({
    02.        dataSource: [
    03.            { text: "Select Report", value: "" },
    04.            { text: "Stakeholder Opinion Report", value: "StakeholderOpinionReport.aspx" },
    05.            { text: "General Landowner Report", value: "GeneralLandownerReport.aspx" }
    06.        ],
    07.        dataTextField: "text",
    08.        dataValueField: "value",
    09.        change: function (e) {
    10.            switch (this.select()) {
    11.                case 1:
    12.                    $("#pnlReportParams").show();
    13.                    $("div[ref=all]").show();
    14.                    $("div[ref=stakeholder]").show();
    15.                    $("div[ref=landowner]").hide();
    16.                    break;
    17.                case 2:
    18.                    $("#pnlReportParams").show();
    19.                    $("div[ref=all]").show();
    20.                    $("div[ref=stakeholder]").hide();
    21.                    $("div[ref=landowner]").show();
    22.                    break;
    23.                default:
    24.                    $("#pnlReportParams").hide();
    25.                    break;
    26.            }
    27.        }
    28.    }).data("kendoDropDownList");

    When the page loads, the dropdownlist shows "[object Object]".  If I click on it, the items in the datasource appear, but selecting any of them results in the "[object Object]" being shown.

     I've tried the "valuePrimitive:true" with no success.

     Using 2016.1.12

     Any help is greatly appreciated!!

  13. Kevin F
    Kevin F avatar
    46 posts
    Member since:
    Sep 2009

    Posted 28 Jan in reply to Kevin F Link to this post

    Disregard with my apologies.  Prior developer had included another kendo script file which of course wasn't necessary.  Removed the include and the dropdownlists work as expected.
Back to Top
UI for ASP.NET MVC is VS 2017 Ready