调用了错误的方法

时间:2013-11-17 16:34:01

标签: c# asp.net asp.net-mvc kendo-ui kendo-grid

我有一个asp.net mvc4页面,其中有一个表单和两个网格,但我的问题是网格触发了错误的方法,我不知道出了什么问题。

我的更新方法是调用update方法,其他方法也会触发错误的方法。

有人可以解决这个问题吗?

<%: 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


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

%>


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


            return Json(StudentRepository.GetSchoolYear(_studNum).ToDataSourceResult(request));
        }



        [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));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult students_Update([DataSourceRequest]DataSourceRequest request, StudentsViewModel students)
        {
            if (students != null)
            {

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

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




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

        [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));
        }
        private void PopulateStudents()
        {
            var dataContext = new SSTSDBDataModelDataContext();
            var categories = dataContext.tblMasterStudents
                        .Select(c => new StudentsViewModel
                        {
                            StudentNumber = c.StudentNumber
                            // StudentNumber=categories
                            // StudentNumber=categories.
                        })
                        .OrderBy(e => e.StudentNumber);
            ViewData["students"] = categories;
            ViewData["defaultCategory"] = categories.First();
        }
        public JsonResult GetPrsoducts(string text)
        {
            var northwind = new SSTSDBDataModelDataContext();


            var products = northwind.tblMasterStudents.Select(product => new StudentsViewModel
            {
                StudentNumber = product.StudentNumber
            });

            if (!string.IsNullOrEmpty(text))
            {
                products = products.Where(p => p.StudentNumber.Contains(text));
            }

            return Json(products, JsonRequestBehavior.AllowGet);
        }



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


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

        [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));
        }
        [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));
        }

        [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));
        }


        //  public tblMasterStudent _stud { get; set; }
}

1 个答案:

答案 0 :(得分:0)

Kendo DataSource可能会调用create方法而不是update方法的一个原因是它认为记录是新的。

如果ID为null或0,则可能认为该记录是新的。请确保此处指定的StudentNumber

model.Id(student => student.StudentNumber);
students_Read返回时,

不为null或0。