Value of DropdownList is null when it has single row in popup grid

5 posts, 0 answers
  1. Adem
    Adem avatar
    13 posts
    Member since:
    Feb 2016

    Posted 03 Mar Link to this post

    I have a grid like this:

    @(Html.Kendo().Grid<License>()
          .Name("popupGrid")
          .Columns(columns =>
          {
              columns.Bound(p => p.LicenseId).Width(20).Hidden().HeaderHtmlAttributes(new { @title = "License" });
              columns.ForeignKey(p => p.CustomerId, (System.Collections.IEnumerable)ViewData["customers"], "CustomerID", "CustomerName")
                .Title("Customer").Width(200);
              columns.Bound(p => p.VendorId).Width(20).HeaderHtmlAttributes(new { @title = "Vendor" });
              columns.Bound(p => p.ProductId).Width(20).HeaderHtmlAttributes(new { @title = "Product" });
              columns.Command(p => p.Edit().Text("Edit").HtmlAttributes(new { @title = "Edit" })).Width(80);
          })
              .ToolBar(toolbar => toolbar.Create().Text("Add").HtmlAttributes(new { @title = "Add" }))
          .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("PopupEditView"))
          .Events(e => e.Edit("onEdit"))
          .DataSource(dataSource => dataSource
              .Ajax()
              .Model(model => model.Id(p => p.LicenseId))
                  .Create(create => create.Action("Create", "Home").Type(HttpVerbs.Post))
                  .Read(read => read.Action("Read", "Home").Type(HttpVerbs.Post))
                  .Update(update => update.Action("Update", "Home").Type(HttpVerbs.Post))
              )
    )

    and PopupEditView.cshtml :

    @(Html.Kendo().DropDownListFor(m => m.CustomerId).Name("CustomerId")
                      .ValuePrimitive(true)
                      .DataTextField("CustomerName")
                      .DataValueField("CustomerId")
                   //.OptionLabel("Select Customer...") 
                      .DataSource(dataSource =>
                      {
                          dataSource.Read(read => read.Action("GetCustomers", "Home"))
                                    .ServerFiltering(true);
                      })
                )
                @Html.ValidationMessageFor(m => m.CustomerId)

    HomeController : 

    [HttpPost]
            public JsonResult Create([DataSourceRequest] DataSourceRequest request, License license)
            {
                if (license != null && ModelState.IsValid) // licence.CustomerId is null Model.State is not valid
                {
                    LicenseRepository.Repository.Insert(license);
                }
     
                return Json(new[] { license }.ToDataSourceResult(request, ModelState));
            }
     
            public JsonResult GetCustomers()
            {
                return Json(CustomerRepository.Repository.Customers, JsonRequestBehavior.AllowGet);
            }

    If .OptionLabel("Select Customer...") is commented  and submit create then licence then CustomerId is null. If user change DropDownList(CustomerId) then it works fine.

    Is this a bug? 

     

  2. Adem
    Adem avatar
    13 posts
    Member since:
    Feb 2016

    Posted 07 Mar in reply to Adem Link to this post

    Hi Telerik Team,

    You can try this issue on here . (telerik_ui-for-aspnet-mvc-examples-master\grid\grid-editing-cascading-dropdownlist) 

     

  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 07 Mar Link to this post

    Hello Adem,

    I've tried to replicate the behavior, but wasn't able to. I'm sending the example which you mention attached to this post. Could you please modify it so that it illustrates the behavior in question?

    Regards,
    Nikolay Rusev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  5. Adem
    Adem avatar
    13 posts
    Member since:
    Feb 2016

    Posted 07 Mar in reply to Nikolay Rusev Link to this post

    I tried your example. But I used ASP.NET MVC 5.2.3 and Kendo.Mvc(latest version)

    I didn't change value of Customer Id. (I didn't handled(touch) DropDownlist of Customer) , only I selected vendorId and Product Id and submitted. 

    But customer Id's value has been 0 instead of 1. 

    You can look at Screenshots.

  6. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 09 Mar Link to this post

    Hello Adem,

     

    I see what you mean now. The value stays 0 as the default value for the field type is 0. 

    That said the correct configuration for the scenario is to define default value for the `CustomerId` field.

     

    Example:

    .DataSource(dataSource => dataSource
              .Ajax()
              .Model(model =>
              {
                  model.Id(p => p.LicenseId);
                  model.Field(p => p.CustomerId).DefaultValue(1);
              })

     

     

     

    Regards,

    Nikolay Rusev

    Telerik

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready