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

Update button works for Edit but not for Create

3 Answers 580 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 08 Jan 2016, 08:08 PM

This grid works for Read, Update (of existing record only) and Delete, but when Creating a new row, the Update button doesn't work. (When creating a new row, the Cancel button does work).

e.g.

After Reading data from server:

 - Click Edit button on existing row. Make changes. Click Update. Success!

 - Click Delete button on existing row. Success!

 - Click Add new record button. New row appears. Click Cancel. Success!

 - Click Add new record button. New row appears. Click Update. Nothing happens. Controller method is not called.

What is wrong?

Thanks

Grid:

 

@(Html.Kendo().Grid<PublicUserMunicipalityViewModel>()
       .Name("publicUserMunicipalitiesGrid")
       .Columns(columns =>
         {
           columns.Bound(u => u.Id).Hidden();
           columns.Bound(u => u.PublicUserId).Hidden();
           columns.Bound(u => u.UserId).Hidden();
           columns.Bound(u => u.muni_code).Hidden();
           columns.Bound(u => u.Municipality).ClientTemplate("#=Municipality.MuniCombo#");
           columns.Bound(u => u.RequestedDate).Hidden();
           columns.Bound(u => u.Granted).Hidden();
           columns.Bound(u => u.CanSubmitForms).ClientTemplate("<input type='checkbox' disabled='disabled' #= CanSubmitForms ? checked='checked' : '' # ></input>");
           columns.Bound(u => u.SuperUser).ClientTemplate("<input type='checkbox' disabled='disabled' #= SuperUser ? checked='checked' : '' # ></input>");
           columns.Bound(u => u.IsCurrent).Hidden();
           columns.Command(command => { command.Edit(); command.Destroy(); }).Width(180);
         })
       .ToolBar(toolbar => toolbar.Create())
       .DataSource(dataSource => dataSource
          .Ajax()
          .Model(model =>
            {
              model.Id(p => p.Id);
              model.Field(p => p.Id);
              model.Field(p => p.PublicUserId);
              model.Field(p => p.UserId);
              model.Field(p => p.muni_code);
              model.Field(p => p.Municipality).DefaultValue(ViewData["defaultMunicipality"] as MunicipalityViewModel);
              model.Field(p => p.RequestedDate);
              model.Field(p => p.Granted);
              model.Field(p => p.CanSubmitForms);
              model.Field(p => p.SuperUser);
              model.Field(p => p.IsCurrent);
            })
          .Read(read => read.Action("PublicUserMunicipalityViewModelsRead", "PublicUserMunicipalityViewModels")
                          .Data("getSelectedPublicUserId"))
          .Create(create => create.Action("PublicUserMunicipalityViewModelsCreate", "PublicUserMunicipalityViewModels")
                              .Data("getSelectedPublicUserId"))
          .Update(update => update.Action("PublicUserMunicipalityViewModelsUpdate", "PublicUserMunicipalityViewModels"))
          .Destroy(destroy => destroy.Action("PublicUserMunicipalityViewModelsDestroy", "PublicUserMunicipalityViewModels"))
       )
)

Controller Method:

public ActionResult PublicUserMunicipalityViewModelsCreate([DataSourceRequest]DataSourceRequest request, PublicUserMunicipalityViewModel publicUserMunicipalityViewModel, int publicUserId)
{
  // Set UserId and PublicUserId
  publicUserMunicipalityViewModel.PublicUserId = publicUserId;
 
  var publicUser = db.PublicUsers.FirstOrDefault(pu => pu.Id == publicUserId);
  publicUserMunicipalityViewModel.UserId = publicUser.UserId;
   
  // Clear the ModelState errors these two required fields had as they were passed in null.
  ModelState["PublicUserID"].Errors.Clear();
  ModelState["UserID"].Errors.Clear();
 
  if (ModelState.IsValid)
  {
    // Create a new PublicUserMunicipality entity and set its properties from the posted PublicUserMunicipalityViewModel
    var entity = this.BuildPublicUserMunicipalityEntity(publicUserMunicipalityViewModel);
 
    // Add the entity
    db.PublicUserMunicipalities.Add(entity);
 
    // Delete the entity in the database
    db.SaveChanges();
 
    // Get the Id generated by the database
    publicUserMunicipalityViewModel.Id = entity.Id;
  }
 
  // Return the inserted contract. Also return any validation errors.
  return Json(new[] { publicUserMunicipalityViewModel }.ToDataSourceResult(request, ModelState));
}

3 Answers, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 12 Jan 2016, 01:27 PM

Hello Greg,

 

The problem is that one of the columns is bound to complex object. In this case a default value should be set for this field. Otherwise it will throws an error when creating a new item ( it will try to get the MuniCombo property of undefined). 

 

Please refer to the Grid / Editing custom editor demo and specifically how a default value is set for the Category field. 

 

Regards,
Boyan Dimitrov
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
Greg
Top achievements
Rank 1
answered on 12 Jan 2016, 02:39 PM

I am setting a default value.

The line from the grid:

model.Field(p => p.Municipality).DefaultValue(ViewData["defaultMunicipality"] as MunicipalityViewModel);

And the View Controller Index method:

public ActionResult Index()
{
  var municipalities = db.fin_muni_codes.Select(f => new MunicipalityViewModel {
            muni_code = f.muni_code,
            MuniCombo = f.muni_name + " - " + f.community_type + " - " + f.muni_code
        })
        .OrderBy(x => x.MuniCombo);
 
  ViewData["municipalities"] = municipalities;
  ViewData["defaultMunicipality"] = municipalities.First();
 
  return View();
}

When I click the Add new record button, a row appears with the first/default municipality in the dropdown.

 

Any other ideas to explore? Thanks.

0
Greg
Top achievements
Rank 1
answered on 12 Jan 2016, 06:03 PM

I found the problem. I had the primary key of the entity defined as both a model.Id and a model.Field.

Changing:

model.Id(p => p.Id);
 model.Field(p => p.Id);

To:

model.Id(p => p.Id);

Fixed the problem.
Tags
Grid
Asked by
Greg
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Greg
Top achievements
Rank 1
Share this question
or