Add new record clear selected DropDownList Columns

2 posts, 0 answers
  1. Daniel
    Daniel avatar
    7 posts
    Member since:
    Jul 2016

    Posted 26 Jan 2017 Link to this post

    Hi,

    I have a Grid MVC with some DropDownList columns. I can select any item from dropdownlists, but when I add new record to grid, the selected items from DropDownList columns are cleared.
    How can I mantain the selected data adding new records to grid?


    Regards

     

    01.@(Html.Kendo().Grid(Model.Subsidiaries)
    02.            .Name("Subsidiaries")
    03.            .ToolBar(tools => tools.Create().Text("Agregar nuevo"))
    04.            .Editable(editable => editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom))
    05.            .Scrollable()
    06.            .Columns(columns =>
    07.            {
    08.                columns.Bound(c => c.SubsidiariesName).ClientTemplate("#=SubsidiariesName #" +
    09.                    "<input type='hidden' name='Subsidiaries[#= indexSubsidiariesCommercial(data)#].SubsidiariesName' value='#= SubsidiariesName #' />"
    10.                );
    11.                columns.Bound(c => c.Address).ClientTemplate("#=Address #" +
    12.                    "<input type='hidden' name='Subsidiaries[#= indexSubsidiariesCommercial(data)#].Address' value='#= Address #' />"
    13.                );
    14.                columns.Bound(c => c.PhoneNumber).ClientTemplate("#=PhoneNumber #" +
    15.                    "<input type='hidden' name='Subsidiaries[#= indexSubsidiariesCommercial(data)#].PhoneNumber' value='#= PhoneNumber #' />"
    16.                );
    17.                columns.Bound(p => p.BusinessUnitsId).ClientTemplate(
    18.                    @Html.DropDownList("Subsidiaries[#= indexSubsidiariesCommercial(data)#].BusinessUnitsId", ViewBag.BusinessUnits as SelectList, "Seleccione",
    19.                    htmlAttributes: new { @class = "form-control" }).ToHtmlString()
    20.                );
    21.                columns.Bound(p => p.Id).ClientTemplate(
    22.                    @Html.DropDownList("Subsidiaries[#= indexSubsidiariesCommercial(data)#].Countries", ViewBag.Countries as SelectList, "Seleccione",
    23.                    htmlAttributes: new { @class = "form-control", @onchange = "changeCountry(this.value, #= indexSubsidiariesCommercial(data)#)" }).ToHtmlString()
    24.                );
    25.                columns.Bound(p => p.Id).ClientTemplate(
    26.                    @Html.DropDownList("Subsidiaries[#= indexSubsidiariesCommercial(data)#].Departments", ViewBag.Departments as SelectList, "Seleccione",
    27.                        htmlAttributes: new { @class = "form-control", @disabled = "disabled", @onchange = "changeDepartment(this.value, #= indexSubsidiariesCommercial(data)#)"
    28.                    }).ToHtmlString()
    29.                );
    30.                columns.Bound(p => p.CitiesId).ClientTemplate(
    31.                    @Html.DropDownList("Subsidiaries[#= indexSubsidiariesCommercial(data)#].CitiesId", ViewBag.Cities as SelectList, "Seleccione",
    32.                    htmlAttributes: new { @class = "form-control", @disabled = "disabled" }).ToHtmlString()
    33.                );
    34.                columns.Bound(c => c.SourceCreated).ClientTemplate("#=SourceCreated #" +
    35.                    "<input type='hidden' name='Subsidiaries[#= indexSubsidiariesCommercial(data)#].SourceCreated' value='#= SourceCreated #' />"
    36.                ).Hidden(true);
    37.                columns.Bound(c => c.UserCreated).ClientTemplate("#=UserCreated #" +
    38.                    "<input type='hidden' name='Subsidiaries[#= indexSubsidiariesCommercial(data)#].UserCreated' value='#= UserCreated #' />"
    39.                ).Hidden(true);
    40.                columns.Command(command => command.Destroy()).Width(100);
    41.            })
    42.            .DataSource(dataSource => dataSource
    43.                .Ajax()
    44.                .ServerOperation(false)
    45.                .Model(model =>
    46.                {
    47.                    model.Id(s => s.Id);
    48.                    model.Field(p => p.UserCreated).DefaultValue(HttpContext.Current.GetOwinContext().
    49.                        Authentication.User.Claims.First(x => x.Type == System.IdentityModel.Claims.ClaimTypes.GivenName).Value);
    50.                    model.Field(p => p.SourceCreated).DefaultValue(HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()
    51.                        + "/" + HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString());
    52.                })
    53.            )
    54.)
  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 30 Jan 2017 Link to this post

    Hi Daniel,

    I am not sure how you are changing the values in the model, but it seems that those changes are not preserved. Please note that you need to bind the DropDownList value to the model. Here is a simple example demonstrating such binding:
    columns.Bound(p => p.UnitPrice).Width(120).ClientTemplate("<input data-role='numerictextbox' data-bind='value: UnitPrice' data-min='-99' style='width: 80px;'/>"); ;

    And within the DataBound event of the Grid you need to bind the model as shown below:
    function dataBound(e) {
        var grid = this;
        grid.element.find("[role='row']").each(function () {
            var tr = $(this).closest('tr');
            var item = grid.dataItem(tr);
            kendo.bind($(this), item);
        });
    }

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top