always 0 then new object

9 posts, 1 answers
  1. Gusev
    Gusev avatar
    74 posts
    Member since:
    Dec 2012

    Posted 24 Sep 2013 Link to this post

    Hello!
    I use Combobox  with Ajax Loading.
    And I have a question:
    1) I have a code:
    @model int
                
    @(Html.Kendo().ComboBoxFor(x => x)
        .DataTextField("Description")
        .DataValueField("Id")
        .Placeholder("Select")
        .HtmlAttributes(new { style = "width:225px", id = Guid.NewGuid().ToString() })
        .DataSource(databinding => databinding
            .Read(read => read
            .Action("SelectComboboxItems", "Lumber")))
    )
    2) If I use DropDownListFor instead of ComboBoxFor...everything well works and DropDownList select OptionLabel (because I dont have item with Id = 0)
    But If I use ComboboxFor...than I get this...(please see  zero.png)
    Why Combobox does this?  I want that combobox set placeholder instead of 0, when I dont have item with Id=0
  2. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 26 Sep 2013 Link to this post

    Hello Gusev,

     
    The described behavior is expected, because the ComboBox widget is designed to accept custom values. The default value of int is 0. When you create new item you will see the default value of the integer as ComboBox value. You can either make Model of the editor template Nullable<int> or to clear the value of the widget when Model is 0.

    Regards,
    Georgi Krustev
    Telerik
    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. Gusev
    Gusev avatar
    74 posts
    Member since:
    Dec 2012

    Posted 26 Sep 2013 Link to this post

    Thanks for your answer Georgi Krustev!
    How I can do this: "Clear the value of the widget when Model is 0." 
    I can subscribe on Event DataBound and then clear value, but how I can do this in EditorTemplate?
  5. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 30 Sep 2013 Link to this post

    Hello again Gusev,

     
    I will suggest you try clear the default value of the integer using the Value method of the wrapper:

    @model int
                 
    @(Html.Kendo().ComboBoxFor(x => x)
        .Value(Model == 0 ? string.Empty : Model.ToString())
        .DataTextField("Description")
        .DataValueField("Id")
        .Placeholder("Select")
        .HtmlAttributes(new { style = "width:225px", id = Guid.NewGuid().ToString() })
        .DataSource(databinding => databinding
            .Read(read => read
            .Action("SelectComboboxItems", "Lumber")))
    )

    Regards,
    Georgi Krustev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Gusev
    Gusev avatar
    74 posts
    Member since:
    Dec 2012

    Posted 30 Sep 2013 Link to this post

    Hello!
    I try this method earlier, but ".Value()" in wrapper Combobox doesn't work for me :(
    If I use .Value(), then nothing changes.
  7. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 01 Oct 2013 Link to this post

    Hello Gusev,

     
    If declaratively value setting does not work, then probably widget's value is set subsequently. In that case I will need more information about your current implementation in order to advice you further.

    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. Gusev
    Gusev avatar
    74 posts
    Member since:
    Dec 2012

    Posted 03 Oct 2013 Link to this post

    my EditorTemplate with name "PlanLumberList"
    @model int
                  
    @(Html.Kendo().ComboBoxFor(x => x)
        .Value(Model == 0 ? string.Empty : Model.ToString())
        .DataTextField("Description")
        .DataValueField("Id")
        .Placeholder("Select")
        .HtmlAttributes(new { style = "width:225px", id = Guid.NewGuid().ToString() })
        .DataSource(databinding => databinding
            .Read(read => read
            .Action("SelectComboboxItems", "Lumber")))
    )
    my field in class
    [Required]
    [UIHint("PlanLumberList")]
    [Display(Name = "IdLumberDisplay", ResourceType = typeof(LocalizationLumber))]
    public virtual int IdLumber { get; set; }
    my grid bound column
    columns.Bound(o => o.IdLumber).ClientTemplate("#= LumberName #").Width(160);


  9. Answer
    Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 04 Oct 2013 Link to this post

    Hello Gusev,

     
    In that case, you can wire the edit event of the grid. Thus you can get the combobox widget and clear the 
    default value:

    function edit(e) {
         var combobox = e.container.find("#PlanLumberList").data("kendoComboBox");
         if ( combobox && combobox.value() === "0" ) {
              combobox.value("");
         }
    }
    Check this link for the other arguments passed to the edit event handler.

    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. Gusev
    Gusev avatar
    74 posts
    Member since:
    Dec 2012

    Posted 04 Oct 2013 Link to this post

    Hello Georgi Krustev!
    Thanks for help. 
Back to Top
UI for ASP.NET MVC is VS 2017 Ready