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

[Solved] Grid Ajax inline Delete not working due to datakey is null

2 Answers 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jon
Top achievements
Rank 1
Jon asked on 22 Oct 2010, 06:44 PM
Hi,
I use Grid control and is having some issue for inline deleing due to the DataKey. Basically when I click "Delete" link, the request does not include datakey (primarykey for entity) I defined in the grid. This causes the problem when invoke the action. My grid and controller action are defined as follows:
<% Html.Telerik().Grid<ehrEHRGUI.Areas.Patient.ViewModels.AllergyForGridViewModel>()
                .Name("AllergyGrid")
                .DataKeys(keys =>
                {
                    keys.Add(a => a.AllergyNo);
                })
                .DataBinding(dataBinding => dataBinding.Ajax()
                       .Select("AjaxAllergyGrid", "PatientHome")
                       .Delete("AjaxAllergyGridDelete", "PatientHome")
                      )
                .Columns(columns =>
                {
                    columns.Bound(allergy => allergy.AllergyCode);
                    columns.Bound(allergy => allergy.AllergyTextDescription).Title("Allergy Agent");
                    columns.Bound(allergy => allergy.AllergyReactionDescription).Title("Reaction");
                    columns.Bound(allergy => allergy.OnSetDateTime).Format("{0:MM/dd/yyyy}").Title("OnSet Date");
                    columns.Bound(allergy => allergy.StatusDescription).Title("Status");
                    columns.Command(commands =>
                    {
                        commands.Delete();
                    }).HtmlAttributes(new { style = "padding:1px;" }).Width(100).Title("Delete");
                })
                .Pageable(paging => paging.PageSize(5).Style(GridPagerStyles.Numeric))
                .Editable(a=>a.Enabled(true).Mode(GridEditMode.InLine))
                .Render(); 
                                    %>
//this is the intial action
public ActionResult PatientAllergies(int userNo, int patientNo)
        {
            var patientAllergyVM = LoadPatientData.LoadAllergyViewModel(userNo, patientNo, _providerService, _patientService);
            return View(patientAllergyVM);
  
            //return View();
        }
  
  
//this is the action called by Ajax.select
[GridAction]
public ActionResult AjaxAllergyGrid(int userNo, int patientNo)
        {
            var allergyForGrid = LoadPatientData.LoadAllergyForGridViewModel(userNo, patientNo, _providerService, _patientService);
            return View(new GridModel<ehrEHRGUI.Areas.Patient.ViewModels.AllergyForGridViewModel
                    {
                        Data = allergyForGrid 
                    });
        }
  
  
  
//this is delete action and it does not get call at all due to allergyNo is null
[HttpPost]
[GridAction]
public ActionResult AjaxAllergyGridDelete(int userNo, int patientNo, int allergyNo)
        {
            var patientAllergy = _patientService.GetPatientAllergyByAllergyNo(allergyNo);
            _patientService.Delete(patientAllergy);
            try
            {
                // Try to save changes, which may cause a conflict.
                _patientService.Save();                                     //save to db, remember to use the same dbcontext with get
            }
            catch (System.Data.OptimisticConcurrencyException)              //in system.data.entity
            {
                // Resolve the concurrency conflict by refreshing the 
                // object context before re-saving changes. 
                _patientService.Refresh("client", patientAllergy);
  
                // Save changes.
                _patientService.Save();
            }
  
            var allergyForGrid = LoadPatientData.LoadAllergyForGridViewModel(userNo, patientNo, _providerService, _patientService);
            return View(new GridModel<ehrEHRGUI.Areas.Patient.ViewModels.AllergyForGridViewModel>
            {
                Data = allergyForGrid
            });
        }

One interest thing is the inline "Delete" shows as link instead of "Button" like the Telerik demo sample. Is there any thing I did it worng in the definition of Grid? Please also see the attach file for screen image. Thanks in advanced.

2 Answers, 1 is accepted

Sort by
0
Jon
Top achievements
Rank 1
answered on 22 Oct 2010, 09:52 PM
Never mind, I resolved it by using RouteKey.
0
Marilyn
Top achievements
Rank 1
answered on 28 Jul 2011, 05:51 AM
I have the same problem but whether I add the RouteKey the whole line becomes an error. it says that tha expression does not produce a value
 i am doing code in Visual Studio 2010 MVC3 VB
The following lines have this  problem
@ModelType IEnumerable(of SubconsultantDB.ContactCountryViewModel)
@(Html.Telerik() _
            .Grid(Model) _
            .Name("Countries_" & ViewBag.Contact_ID) _
            .DataKeys(Function(keys) _
               { _
                   keys.Add(Function(c) c.Contact_ID).RouteKey("Contact_ID"), _
                   keys.Add(Function(c) c.Country_ID).RouteKey("Country_ID") _
               }) _
            .ToolBar(Function(commands) commands.Insert().ButtonType(GridButtonType.Text)) _
            .DataBinding(Function(dataBinding) _
               { _
                   dataBinding.Ajax() _
                        .Select("_CountriesAjax", "Home", New With {.Contact_ID = ViewBag.Contact_ID}) _
                        .Insert("_InsertAjaxEditing", "ContactCountry") _
                        .Update("_SaveAjaxEditing", "ContactCountry") _
                        .Delete("_DeleteAjaxEditing", "ContactCountry") _
               }) _
            .Columns(Function(columns) _
               { _
                   columns.Bound(Function(o) o.CountryName).Width(100), _
                   columns.Command(Function(commands) _
                        { _
                            commands.Edit().ButtonType(GridButtonType.Text), _
                            commands.Delete().ButtonType(GridButtonType.Text) _
                        }).Width(200).Title("Command") _
               }) _
            .Editable(Function(editing) editing.Mode(GridEditMode.InLine)) _
            .Pageable() _
            .Sortable()
)

this grid is render patial because the main screen has a tabstrip and each tabstrip has it own grid
could you help me.
how do you use the Routekey in VB? Please help
Tags
Grid
Asked by
Jon
Top achievements
Rank 1
Answers by
Jon
Top achievements
Rank 1
Marilyn
Top achievements
Rank 1
Share this question
or