【问题标题】:Wrong mehods are called调用了错误的方法
【发布时间】:2013-11-17 16:34:01
【问题描述】:

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

我的更新方法正在调用更新方法,而其他方法也在触发错误的方法。

谁能解决这个问题?

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

【问题讨论】:

  • 请发布您期望发生的事情以及发生的事情。 “错误的方法”信息不足。
  • 当我调用 BoardingPoints_Update 时,BoardingPoints_create 方法在我调用 read 方法时调用类似,然后其他方法正在调用,并且我的其他网格也发生了同样的事情
  • student_update 调用 student_ucreate 方法
  • 谁能帮帮我

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


【解决方案1】:

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

如果 ID 为 null 或 0,它可能会认为记录是新记录。确保在此处指定您的 StudentNumber

model.Id(student => student.StudentNumber);

students_Read返回时不为null或0。

【讨论】:

  • 是的,它不是 null 也不是 0 有时它调用正确的方法有时不是有时它调用 create 方法两次
  • 当它调用create方法而不是update时,看看它是否传递了一个正确的StudentNumber,或者它是否为null或0。可能你的DataSource中有重复记录,并且有一个空白ID .您可以在 devtools 控制台中编写一些 javascript 以使数据源脱离网格,循环遍历所有记录,并查看 .isNew() 是否为其中任何一个返回 true。
  • 你能多解释一下我是asp,net mvc的新手,如果你能给我一些很棒的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多