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

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

4 Answers 482 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adem
Top achievements
Rank 1
Adem asked on 03 Mar 2016, 12:49 PM

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? 

 

4 Answers, 1 is accepted

Sort by
0
Adem
Top achievements
Rank 1
answered on 07 Mar 2016, 07:29 AM

Hi Telerik Team,

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

 

0
Nikolay Rusev
Telerik team
answered on 07 Mar 2016, 01:37 PM
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
0
Adem
Top achievements
Rank 1
answered on 07 Mar 2016, 02:26 PM

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.

0
Nikolay Rusev
Telerik team
answered on 09 Mar 2016, 09:24 AM

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
Tags
Grid
Asked by
Adem
Top achievements
Rank 1
Answers by
Adem
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or