-1

我是 asp.net mvc 的新手,所以我对 kendo mvc 网格有一些问题。这是我的模型:

 public class LessonsDep
    {
      public int LesId { get; set; }        
      public int Activated { get; set; }       
      public string TaskTable { get; set; }
    }
 public class LessonsBusinessLayer
    {
       public void changeLessons(LessonsDep lessons){
       string connectionString =   ConfigurationManager.ConnectionStrings["nisa1415"].ConnectionString;

  using (SqlConnection con = new SqlConnection(connectionString))
            {                
            SqlCommand cmd = new SqlCommand("dep.edidBiology",con);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter paramId = new SqlParameter();
            paramId.ParameterName = "@LesId";
            paramId.Value = LessonNameClass.stcLesId;
            cmd.Parameters.Add(paramId);

            SqlParameter paramActivated = new SqlParameter();
            paramActivated.ParameterName = "@Activated";
            paramActivated.Value = lessons.Activated;
            cmd.Parameters.Add(paramActivated);

            SqlParameter paramTaskTable = new SqlParameter();
            paramTaskTable.ParameterName = "@TaskTable";
            paramTaskTable.Value = lessons.TaskTable;
            cmd.Parameters.Add(paramTaskTable);

            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
  }

///------------------------------------------------ - - - - - - - - -/// 风景:

     @model IEnumerable<BusinessLayer.LessonsDep>
     <div id="clientsDb">
     @(Html.Kendo().Grid(Model)
         .Name("grid")
         .Scrollable()
         .Columns(columns =>
         {
        columns.Bound(c => c.LesId).Width(140);           
        columns.Bound(c => c.Activated).Width(50);           
        columns.Bound(c => c.TaskTable).Width(300);
    })
    .HtmlAttributes(new { style = "height: 500px;" })

    .Sortable()
    .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(5))
    .ToolBar(toolbar =>
    {            
        toolbar.Save();
    })
    .Editable(editable => editable.Mode(GridEditMode.InCell))
        .DataSource(dataSource => dataSource
        .Ajax()
        .Batch(true)

        .ServerOperation(false)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(c => c.LesId))
        .Read("Editing_Read", "LessonController")
        .Update("Editing_Update", "Lesson")            
      )
  )

/ -------------------------------------------------- ------ / 和控制器:

   public ActionResult Index2()  
     {            
        LessonsBusinessLayer lessonsBusinessLayer = new LessonsBusinessLayer();
        List<LessonsDep> lessons = lessonsBusinessLayer.LessonsDeps.ToList();
        string myString = LessonNameClass.LessonsName;           
        return View(lessons);
    }

在这里我想添加应该更新数据的方法:

  public ActionResult Editing_Update()  
     {            
        //.......Can I call ChangeLesson() Method from LessonsBusinessLayer? 
        //if answer is: yes then How i should call this method?
        return View();
     }
4

2 回答 2

0

嘿伙计们,我发现程序的问题是什么。我在这里发布我的答案,也许它会对某人有所帮助。所以答案就在这里: 1. 据我了解,我的数据是服务器绑定(不是 ajax):

        SqlCommand cmd = new SqlCommand("dep.edidBiology",con);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter paramId = new SqlParameter();
        paramId.ParameterName = "@LesId";
        paramId.Value = LessonNameClass.stcLesId;
        cmd.Parameters.Add(paramId);

所以我需要从这里更改代码:

     @(Html.Kendo().Grid(Model)
     .Name("grid")
     .Scrollable()
     .Columns(columns =>
     {
         columns.Bound(c => c.LesDepId).Width(140);
         columns.Bound(c => c.TeId).Width(300);
         columns.Bound(c => c.GradeId).Width(300);
         columns.Bound(c => c.Activated).Width(100);             
         columns.Bound(c => c.GroupId).Width(300);         
         columns.Bound(c => c.TaskTable).Width(300);
         columns.Command(command => command.Edit()).Width(200);
       })
      .HtmlAttributes(new { style = "height: 500px;" })
      .Sortable()
      .Pageable(pageable => pageable
      .Refresh(true)
      .PageSizes(true)
      .ButtonCount(5))   
      .Editable(editable => editable.Mode(GridEditMode.InLine))
      .DataSource(dataSource => dataSource
      *.Ajax()* 
      *.ServerOperation(false)*        
      .Model(model => model.Id(c => c.LesId))        
      .Read("Index2", "Lesson")
      .Update("Editing_Update", "Lesson")
      )
     )

对此:

   @(Html.Kendo().Grid(Model)
   .Name("grid")
   .Scrollable()
   .Columns(columns =>
    {
     columns.Bound(c => c.LesDepId).Width(140);
     columns.Bound(c => c.TeId).Width(300);
     columns.Bound(c => c.GradeId).Width(300);
     columns.Bound(c => c.Activated).Width(100);             
     columns.Bound(c => c.GroupId).Width(300);         
     columns.Bound(c => c.TaskTable).Width(300);
     columns.Command(command => command.Edit()).Width(200);
     })
     .HtmlAttributes(new { style = "height: 500px;" })
     .Sortable()
     .Pageable(pageable => pageable
     .Refresh(true)
     .PageSizes(true)
     .ButtonCount(5))   
     .Editable(editable => editable.Mode(GridEditMode.InLine))
     .DataSource(dataSource => dataSource
      *.Server()* 
      *.ServerOperation(false)*------>>>>> delete this line        
      .Model(model => model.Id(c => c.LesId))        
      .Read("Index2", "Lesson")
      .Update("Editing_Update", "Lesson")
     )
   )

我的从剑道网格接收数据的控制器看起来像:

     public ActionResult Index()
    {            
        LessonsBusinessLayer lessonsBusinessLayer = new LessonsBusinessLayer();
        List<LessonsDep> lessons = lessonsBusinessLayer.LessonsDeps.ToList();          
        return View(lessons);
    }        

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Editing_Update([DataSourceRequest] DataSourceRequest request, LessonsDep product)
    {
        if (product != null && ModelState.IsValid)
        {
            LessonsBusinessLayer lessonsBusinessLayer = new LessonsBusinessLayer();
            lessonsBusinessLayer.changeLessons(product);                                
            return RedirectToAction("Index");
        }
        return View();
    }
于 2014-08-29T11:29:13.103 回答
0

你必须改变

.Editable(editable => editable.Mode(GridEditMode.InCell)) 

.Editable(editable => editable.Mode(GridEditMode.InLine))

按照以下方式编写控制器..

控制器

public JsonResult SaveAccountAdmin([DataSourceRequest]DataSourceRequest request,CompanyContactModel companyContactModel)
{
If error: ModelState.AddModelError(string.Empty, e.Message);
DataSourceResult result = [Your Model List].ToDataSourceResult(request, ModelState);
return Json(result, JsonRequestBehavior.AllowGet);
}

我希望这对你有帮助..

于 2014-08-26T13:30:48.833 回答