kendo mvc网格更新

时间:2014-08-26 10:20:52

标签: kendo-grid kendo-asp.net-mvc kendo-datasource

我是asp.net mvc的新手,所以我对kendo mvc grid有些问题。 在这里我的模特:

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

2 个答案:

答案 0 :(得分: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);
}

我希望这能帮到你..

答案 1 :(得分: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")
     )
   )

我从kendo网格接收数据的控制器如下所示:

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