kendo mvc grid updating - kendo-grid
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();
}
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..
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();
}
Related
How to pass object as parameter to kendo grid read method
I have kendo grid as follow. #(Html.Kendo().Grid<ManualInputDetail>() .Name("gManualInputDetail") .Columns(columns => { columns.Bound(c => c.Id).Hidden(true); columns.Bound(c => c.Month).Title("Month"); columns.Bound(c => c.Value).Title("Value"); }) .Editable(editable => editable.Mode(GridEditMode.InLine)) .Pageable() .Navigatable() .Selectable(selectable => { selectable.Mode(GridSelectionMode.Single); selectable.Type(GridSelectionType.Row); }) .Sortable(sortable => { sortable.SortMode(GridSortMode.MultipleColumn); }) .DataSource(dataSource => dataSource .WebApi() .Model(model => model.Id(p => p.Id)) .PageSize(12) .Read(read => read.Url(Url.HttpRouteUrl("ActionApi", new { controller = "ManualInputDetails", action = "GetManualInputDetails" })).Data("getFilterData")) ) .Pageable(p => p.Refresh(true)) ) using getFilterData function I want to pass object parameter to read method. getFilterData function as follow function getFilterData() { var header= { SectorId: 1, BrandId: 2, LocationId: 1, DataElementId:2 } return { header: header }; } GetManualInputDataElements method as follow [ActionName("GetManualInputDetails")] public DataSourceResult GetManualInputDetails([System.Web.Http.ModelBinding.ModelBinder(typeof(WebApiDataSourceRequestModelBinder))] DataSourceRequest request,ManualInputHeader header) { var model = new DataElementMgt().GetAll(header).Select(x => new DataElement() { Id = x.Id, DataElementTypeId = x.DataElementTypeId, Name = x.Name, Descriptionn = x.Descriptionn }).ToList().ToDataSourceResult(request); return model; } In here header value always gives as null. What is the reason for that. Is any thing wrong? Please help..
change the getFilterData method to this function getFilterData() { var _header= { SectorId: 1, BrandId: 2, LocationId: 1, DataElementId:2 } return { header: _header }; } and it should work. dont use the same name for what you return and declare.
MVC Telerik grid not binding only displaying json data in browser
I am trying to get a telerik grid to display json data that is being return from a controller action but the only it displays the actual json data in the browser window. Am i supposed to call .BindTo after read? Am i doing something wrong in my action? am i going about this all wrong? [HttpGet] public ActionResult ReadLeads([DataSourceRequest]DataSourceRequest request) { var model = new RecordLookupViewModel(); using (var db = new RGI_MasterEntities()) { db.Configuration.ProxyCreationEnabled = false; var results = db.tblMasterLeads .Where( x => (model.FirstName == null || x.FirstName.Equals("Eric")) && (model.RecordType == null || x.MasterLeadType.Equals("Responder")) ) .Select(s => new LookupGridResults { FirstName = s.FirstName, LastName = s.LastName, City = s.city, State = s.state, County = s.county, Zip = s.zip }).Take(10); var result = results.ToDataSourceResult(request); return Json(result, JsonRequestBehavior.AllowGet); } } Hers is my view code for the grid. #(Html.Kendo().Grid<LookupGridResults>() .Name("grid") .AutoBind(false) .Columns(columns => { columns.Bound(p => p.FirstName).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains"))).Width(225); columns.Bound(p => p.LastName).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains"))); columns.Bound(p => p.City).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains"))); columns.Bound(p => p.County).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains"))); columns.Bound(p => p.State).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains"))); columns.Bound(p => p.Zip).Width(225).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains"))); }) .Pageable() .Sortable() .Scrollable() .Filterable(ftb => ftb.Mode(GridFilterMode.Row)) .HtmlAttributes(new { style = "height:550px;" }) .DataSource(dataSource => dataSource .Ajax() .PageSize(20) .ServerOperation(true) .Read(read => read.Action("ReadLeads", "LeadsManagement").Type(HttpVerbs.Get)) ) ) Here are my results btw. {"Data":[{"LastName":"COFFEY","FirstName":"EDWARD","City":"FRANKFORT","County":"FRANKLIN","State":"KY","Zip":"40601-2304"},{"LastName":"DESPAIN","FirstName":"TONY","City":"CAMPBELLSVILLE","County":"TAYLOR","State":"KY","Zip":"42718-9397"},{"LastName":"HALBIG","FirstName":"RONALD","City":"CAMPBELLSVILLE","County":"TAYLOR","State":"KY","Zip":"42718-1556"},{"LastName":"KRAUS","FirstName":"REBECCA","City":"FRANKFORT","County":"FRANKLIN","State":"KY","Zip":"40601-2714"},{"LastName":"LAWLESS","FirstName":"MEREDITH","City":"CAMPBELLSVILLE","County":"TAYLOR","State":"KY","Zip":"42718-1556"},{"LastName":"RANKIN","FirstName":"PAULINE","City":"LAWRENCEBURG","County":"ANDERSON","State":"KY","Zip":"40342-1374"},{"LastName":"SHIRLEY","FirstName":"LORRAINE","City":"CAMPBELLSVLLE","County":"TAYLOR","State":"KY","Zip":"42718-1557"},{"LastName":"STAPLES","FirstName":"DAMON","City":"HODGENVILLE","County":"LARUE","State":"KY","Zip":"42748-1208"},{"LastName":"WILLIAMS","FirstName":"LUCY","City":"FRANKFORT","County":"FRANKLIN","State":"KY","Zip":"40601-2308"},{"LastName":"WILSON","FirstName":"BELIDA","City":"FRANKFORT","County":"FRANKLIN","State":"KY","Zip":"40601-1321"}],"Total":10,"AggregateResults":null,"Errors":null}
Thanks for all the help, it seemed i was missing a reference to a bundle. I do credit Mark Schultheiss for pointing me in the right direction. Got it completly working today. Here is what fixed it. I changed my actionresult to a JsonResult. I had filtering turned on in the grid but none of my columns had filtering attributes. I think thats about it. It works great now.
How to bind kendo telrick grid dynamicale using mvc?iam new to mvc i dont know where iam going wrong
This is the model part:- I have taken one list and string list for column value and string for column hearder public class MRAprofileGridNew { public string DBColumnName { get; set; } public List<string> DBColumnvalue { get; set; } } This is CONTROLLER parts:-model is return over here public ActionResult RiskScorePatientData([DataSourceRequest]DataSourceRequest request, string ACOName) { return Content(reportRepository.MedicalRiskScorePatientData(User.EmailId, ACOName).KendoJsonResult(request), "application/json"); } View Part:- Her i want to bind the data dynamicale here i have struck how to bind data dynamicale #*#if (!string.IsNullOrEmpty(Model)) {*# #(Html.Kendo().Grid<Guardian.Core.Model.ViewModels.MRAprofileGridNew>() .TableHtmlAttributes(new { #class = "table table-condensed Patient-Grid" }) .Name(gridId) .ToolBar(tb => { tb.Excel().HtmlAttributes(new { #class = "btn btn-danger btn-xs pull-right " }); }) .Columns(columns => { // foreach (var ColumnName in Model) // { // columns.Bound(gl => gl.DBColumnvalue).Title(ColumnName.DBColumnName).Width(200).Filterable(false).Sortable(false); // } columns.Bound(team => team.DBColumnName).Title("Team Name"); columns.Bound(team => team.DBColumnvalue); //foreach (var tem in Model) //{ // string blabal = tem.DBColumnName.ToString(); // columns.Template(gl => gl.DBColumnvalue[0]).Title(blabal).Width(100); //} //columns.Bound(p => p.FirstName).Title("First Name").Width(90); //columns.Bound(p => p.LastName).Title("Last Name").Width(90); //columns.Bound(p => p.HICN).Title("HICN").Width(70).HtmlAttributes(new { #class = "text text-left" }); //columns.Bound(p => p.year).Title("Year").Width(50).HtmlAttributes(new { #class = "text text-right" }); //columns.Bound(p => p.ProviderName).Title("Provider Name").Width(200); //columns.Bound(p => p.ACOID).Title("ACOID").Width(50).HtmlAttributes(new { #class = "text text-left" }); //columns.Bound(p => p.V22MedicalRiskScore).Width(10).HtmlAttributes(new { #class = "text text-right" }).HeaderTemplate("<span class='kh-Grid-tooltip' data-toggle='tooltip' data-placement='top' title='CMS-HCC V22 Risk Score'>CMS-HCC V22 RS</span>"); //columns.Bound(p => p.RxRiskScore).Width(20).HtmlAttributes(new { #class = "text text-right" }).HeaderTemplate("<span class='kh-Grid-tooltip' data-toggle='tooltip' data-placement='top' title='RxRiskScore'>RxRscore</span>"); }) .Excel(excel => excel .FileName("") .Filterable(true) .AllPages(true)) .Filterable() .Pageable() .Sortable() .Events(events => events.DataBound("onDataBoundPreservePatientGrid")) .Filterable() .DataSource(dataSource => dataSource .Ajax() .PageSize(10) .Read(read => read.Action("RiskScorePatientData", "Reports", new { ACOName = Convert.ToString(ViewBag.CurrAcoName) })) .ServerOperation(false) ) .Resizable(resize => resize.Columns(true)) .Reorderable(reorder => reorder.Columns(true)) ) #*}*#
Kendo Grid simple ajax binding and sorting
The error that i have received when tried to sort the Kendo grid by clicking on the Customer ID column is: {"Invalid property or field - 'CustomerID' for type: OMS_CUSTOMER"}. What else is needed to bind the columns of grid, model view and model. Thanks controller _read function: IQueryable<OMS_CUSTOMER> CustomerList = this.dbContext.OMS_CUSTOMERs; DataSourceResult result = CustomerList.ToDataSourceResult(request , ModelState , c => new CustomerViewModel { CustomerID = c.OMS_CUSTOMER_ID, CustomerName = c.CUSTOMERNAME }); return Json(result); View: #model IEnumerable<NCBA.ViewModels.CustomerViewModel> #(Html.Kendo().Grid<NCBA.ViewModels.CustomerViewModel>() .Name("grid-CustomerViewModel") .DataSource(dataSource => dataSource .Ajax() .Model( model => { model.Id(cust => cust.CustomerID); } ) .Create(create => create.Action("_Create", "Customer")) .Read(read => read.Action("_Read", "Customer")) .Update(update => update.Action("_Update", "Customer")) .Destroy(destroy => destroy.Action("_Delete", "Customer")) ) .Columns(columns => { columns.Bound(c => c.CustomerID); columns.Bound(c => c.CustomerName); columns.Command(commands => { commands.Edit(); commands.Destroy(); }).Title("Commands").Width(200); }) .ToolBar(toolbar => toolbar.Create()) .Editable(editable => editable.Mode(GridEditMode.InLine)) .Pageable() .Sortable() )
Kendo Grid Read Action Not Passing Data To Controller Via the .Data Method Call
I have a Kendo Grid (MVC Razor) that I am trying to have pass extra data to the controller via the .Data call off of the Read method: #(Html.Kendo().Grid<AssignedSiteGridPoco>() .Name("UnAssignedSiteGrid") .Filterable() .Sortable() .Pageable(pageable => pageable .Refresh(true) .PageSizes(true) .ButtonCount(5)) .Columns(columns => { columns.Bound(p => p.SiteId).Hidden(); columns.Bound(p => p.SiteName).Title("Site Name").Width(180); columns.Bound(p => p.City).Title("City").Width(80); columns.Bound(p => p.StateName).Title("State Name").Width(100); columns.Command(command => command .Custom("Add") .Click("unassignedSiteGridClick") ).Width(90); }) .Scrollable() .Selectable(selectable => selectable.Mode(GridSelectionMode.Single)) .Resizable(resize => resize.Columns(true)) .Events(events => { //events.Change("GridChange"); //events.DataBound("OnDataBound"); }) .DataSource(dataSource => dataSource .Ajax() .Model(model => { model.Id(p => p.SiteId); model.Field(p => p.SiteName); model.Field(p => p.City).Editable(false); model.Field(p => p.StateName).Editable(false); }) .PageSize(15) .Read(read => read.Action("GetUnassignedSiteGridDataList", "Manager").Data("getUserRightName")) )) The Javascript block, placed above the grid div, is as follows: function getUserRightName() { return { UserRightName : "2" }; } And the controller: public JsonResult GetUnassignedSiteGridDataList([DataSourceRequest]DataSourceRequest request, string UserRightName) { var model = new AssignedSiteGridPoco(); var siteList = _managerPresentationService.GetUnassignedSiteGridDataList(model); return Json(siteList.ToDataSourceResult(request)); } The string value being passed from the view's read method is null in the "UserRightName" string. According to the examples, it should pass back the text value "2" My Version of Kendo is: 2014.2.807 InternalBuild. Is there a problem in this build in this area? Thanks, Steven
Wrap the key name in the json structure into quotes i.e. change your js function to this: function getUserRightName() { return {'UserRightName':"2"}; }