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

Wrong methods are fired

3 Answers 243 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Garry
Top achievements
Rank 1
Garry asked on 17 Nov 2013, 04:03 PM
I have a ap.net mvc4 page in which there is one form and two grids but my problem is that grids are firing the wrong methods and i have no clue what is wrong can someone please help me out i had the similar problem in the past i created a page again with the same code anf it worked but now i am not able to solve this problem. My update method is calling the update method and other methods are firing wrong methods as well please help me out


<kendo:grid>
<%: Html.Kendo().Grid<SSTS.Models.StudentsViewModel>()
    .Name("grid")
          .Columns(columns =>
          {
              // columns.Bound(student => student.CustomerName);
              columns.Bound(student => student.SchoolYear).Width(160);
        
          
              columns.Bound(student => student.YearStart).Width(50);
              columns.Bound(student => student.YearEnd).Width(50);
                columns.Command(commands =>
              {
                  commands.Edit(); // The "edit" command will edit and update data items
                  commands.Destroy(); // The "destroy" command removes data items
              }).Title("Commands").Width(50);
          })
          .ToolBar(toolbar => toolbar.Create()) // The "create" command adds new data items
          .Editable(editable => editable.Mode(GridEditMode.InLine)) // Use inline editing mode
          .DataSource(dataSource =>
              dataSource.Ajax()
                .Model(model =>
                {
                    model.Id(student => student.StudentNumber); // Specify the property which is the unique identifier of the model
                //    model.Field(student => student.StudentNumber).Editable(false); // Make the studentID property not editable

                model.Field(p => p.StudentNumbers).DefaultValue(
                      ViewData["defaultCategory"] as SSTS.Models.StudentNumbersViewModel);
         
                
                })
                .Create(create => create.Action("students_Create", "Student")) // Action invoked when the user saves a new data item
                .Read(read => read.Action("students_Read", "Student"))  // Action invoked when the grid needs data
                .Update(update => update.Action("students_Update", "Student"))  // Action invoked when the user saves an updated data item
                .Destroy(destroy => destroy.Action("students_Destroy", "Student")) // Action invoked when the user removes a data item
          )
              .Pageable()
%>




<%: Html.Kendo().Grid<SSTS.Models.StudentsViewModel>()
    .Name("grid2")
          .Columns(columns =>
          {
              // columns.Bound(student => student.CustomerName);
              columns.Bound(student => student.BoardingPoint);//.ClientTemplate("#=CustomerNames.CustomerName#").Width(160);
          
              columns.Bound(student => student.StudentNumber).ClientTemplate(
    "<a href='" +
        Url.Action("ViewStudent", "Home") +
        "/#= StudentNumber #'" +
    ">#= StudentNumber #</a>"
); ;
          
            
              columns.Bound(student => student.Description).Width(250);
              columns.Bound(student => student.NumberofSections).Width(250);
              columns.Bound(student => student.ContactNumber).Width(250);
                columns.Command(commands =>
              {
                  commands.Edit(); // The "edit" command will edit and update data items
                  commands.Destroy(); // The "destroy" command removes data items
              }).Title("Commands").Width(200);
          })
          .ToolBar(toolbar => toolbar.Create()) // The "create" command adds new data items
          .Editable(editable => editable.Mode(GridEditMode.InLine)) // Use inline editing mode
          .DataSource(dataSource =>
              dataSource.Ajax()
                .Model(model =>
                {
                    model.Id(student => student.StudentNumber); // Specify the property which is the unique identifier of the model
                //    model.Field(student => student.StudentNumber).Editable(false); // Make the studentID property not editable

              //      model.Field(p => p.CustomerNames).DefaultValue(
               //         ViewData["defaultCategory"] as KendoGridAjaxEditing2.Models.CustomerNamesViewModel);
         
                
                })
                    .Create(create => create.Action("BoardingPoints_Create", "Student")) // Action invoked when the user saves a new data item
                    .Read(read => read.Action("BoardingPoints_Read", "Student"))  // Action invoked when the grid needs data
                    .Update(update => update.Action("BoardingPoints_Update", "Student"))  // Action invoked when the user saves an updated data item
                    .Destroy(destroy => destroy.Action("BoardingPoints_Destroy", "Student")) // Action invoked when the user removes a data item
          )
          .Pageable()

%>




   [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult students_Create([DataSourceRequest]DataSourceRequest request, StudentsViewModel students)
            {
                var results = new List<StudentsViewModel>();

                if (students != null )//&& ModelState.IsValid)
                {
                       _studNum = HttpContext.Session["StudentNumber"].ToString();
                    StudentRepository.InsertSchoolYear(students, _studNum);
                    results.Add(students);

                }

                return Json(results.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
            }
            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult students_Update([DataSourceRequest]DataSourceRequest request, StudentsViewModel students)
            {
                if (students != null && ModelState.IsValid)
                {

                    _studNum = HttpContext.Session["StudentNumber"].ToString();

                    StudentRepository.UpdateSchoolYear(students);
                    UpdateModel(students);




                }
                return Json(new[] { students }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
            }

            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult students_Destroy([DataSourceRequest]DataSourceRequest request, StudentsViewModel product)
            {
                _studNum = HttpContext.Session["StudentNumber"].ToString();

                StudentRepository.DeleteSchoolYear(product);



                return Json(new[] { product }.ToDataSourceResult(request, ModelState));
            }
        




            public ActionResult BoardingPoints_Read([DataSourceRequest] DataSourceRequest request)
            {
                _studNum = HttpContext.Session["StudentNumber"].ToString();


                return Json(StudentRepository.GetSBoardingPoint(_studNum).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
            }

            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult BoardingPoints_Create([DataSourceRequest]DataSourceRequest request, StudentsViewModel students)
            {
                var results = new List<StudentsViewModel>();

                if (students != null )
                {
                      _studNum = HttpContext.Session["StudentNumber"].ToString();
                    StudentRepository.InsertBoardingPoint(students, _studNum);
                    results.Add(students);

                }

                return Json(results.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
            }
            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult BoardingPoints_Update([DataSourceRequest]DataSourceRequest request, StudentsViewModel students)
            {
                if (students != null )
                {
                    _studNum = HttpContext.Session["StudentNumber"].ToString();


                    StudentRepository.UpdateBoardingPoint(students);
                    UpdateModel(students);




                }
                return Json(new[] { students }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
            }

            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult BoardingPoints_Destroy([DataSourceRequest]DataSourceRequest request, StudentsViewModel product)
            {

                _studNum = HttpContext.Session["StudentNumber"].ToString();
                StudentRepository.DeleteBoardingPoint(product);



                return Json(new[] { product }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
            }

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 20 Nov 2013, 07:55 AM
Hello,

Which methods are fired instead of the correct ones? If the create action is being called instead of updated or destroy, then this indicates that the field defined as ID in the model, is not set and is equal to the default value.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
VIRUX
Top achievements
Rank 1
answered on 25 Sep 2014, 03:03 AM
Same issue facing.
- calling wrong method , (when update => called create, sometime twice)
Add in default value will solve all these.
.Model(model => {           
                             model.Id(p => p.ID);  // primary key
                              model.Field(p => p.ID).DefaultValue(9999); //!Important
                            })
I still have no idea about detail cause but this will solve.
0
Daniel
Telerik team
answered on 26 Sep 2014, 10:52 AM
Hi,

The reason is how a record is determined to be new or existing one:

The id field is used to determine if a model instance is new or existing one. If the value of the field specified is equal to the default value (specified through the fields configuration) the model is considered as new.

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Garry
Top achievements
Rank 1
Answers by
Daniel
Telerik team
VIRUX
Top achievements
Rank 1
Share this question
or