Update button works for Edit but not for Create

4 posts, 0 answers
  1. Greg
    Greg avatar
    35 posts
    Member since:
    Sep 2013

    Posted 08 Jan 2016 Link to this post

    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).


    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?




           .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
              .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")
              .Create(create => create.Action("PublicUserMunicipalityViewModelsCreate", "PublicUserMunicipalityViewModels")
              .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.
      if (ModelState.IsValid)
        // Create a new PublicUserMunicipality entity and set its properties from the posted PublicUserMunicipalityViewModel
        var entity = this.BuildPublicUserMunicipalityEntity(publicUserMunicipalityViewModel);
        // Add the entity
        // Delete the entity in the database
        // 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));

  2. Boyan Dimitrov
    Boyan Dimitrov avatar
    2026 posts

    Posted 12 Jan 2016 Link to this post

    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. 


    Boyan Dimitrov
    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
  3. Greg
    Greg avatar
    35 posts
    Member since:
    Sep 2013

    Posted 12 Jan 2016 in reply to Boyan Dimitrov Link to this post

    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.

  4. Greg
    Greg avatar
    35 posts
    Member since:
    Sep 2013

    Posted 12 Jan 2016 Link to this post

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


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


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

    Fixed the problem.
Back to Top