-1

I am new to asp.net mvc so i have some problems with kendo mvc grid. here my model:

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

///----------------------------------------------------------------/// the view:

     @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")            
      )
  )

/-------------------------------------------------------/ and the controller :

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

here i want to add method that shoul update data:

  public ActionResult Editing_Update()  
     {            
        //.......Can I call ChangeLesson() Method from LessonsBusinessLayer? 
        //if answer is: yes then How i should call this method?
        return View();
     }
  • I have LessonsBusinessLayer class and ChangLesson method in it. This method updates data in sqlserver. I want to call this method in controller when user clicks update button on kendo.grid. How can i do it? – Жарас Әнуарбекұлы Aug 26 '14 at 12:50

2 Answers2

0

You have to change

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

to

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

Write Controller as per below..

Controller

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

I hope this help you..

Parthiv Pandya
  • 336
  • 2
  • 11
0

Hey guys I found what's the problem of programm. I post my answer here , maybe it will help to someone. So answer is here: 1. as i understood my data is server binding (not 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);

so i need to change code in view from this:

     @(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")
      )
     )

to this:

   @(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")
     )
   )

and my controller that receives data from kendo grid looks like:

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