Kendo Grid command.custom url - asp.net-mvc

I am trying to get command.custom to work with a url. i have command.Custom("Tasks"); and it creates the button like it should. But i can't figure out how to get it to point to this url ../../OBClientSetupTasks/Index/#item.SetupID
I tried to make it do a .action and click through the controller and view but it returns a not supported exception
command.Custom("Tasks").Action("Index", "OBClientSetup");
I also can't seem to identify #item.SetupID . item is usually from a foreach, but in this case I don't see where a foreach would go.
EDIT:
Adding full grid
#(Html.Kendo().Grid<XXX_2_0_OBOE.OpenAccess.OBClientSetup>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(obcs => obcs.ProfileName);
columns.Bound(obcs => obcs.Default).ClientTemplate("#= Default ? 'Yes' : 'No' #");
columns.Bound(obcs => obcs.EEFinalize).ClientTemplate("#= EEFinalize ? 'Yes' : 'No' #");
columns.Bound(obcs => obcs.AllowOutsideCodes).ClientTemplate("#= EEFinalize ? 'Yes' : 'No' #");
columns.Bound(obcs => obcs.Completed).ClientTemplate("#= Completed ? 'Yes' : 'No' #");
columns.Command(command =>
{
command.Custom("Tasks").Action("Index", "OBClientSetup", new { SetupID = Model.SetupID });
command.Edit();
command.Destroy().HtmlAttributes(new { #class = "onboard-delete " });
});
})
.ToolBar(toolbar =>
{
toolbar.Template(#<text>
<div class="toolbar">
<div class="row">
<div class="col-md-3 pull-right">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></span>
<input type="text" class="form-control" id='FieldFilter' placeholder="Search Profile Name">
</div>
</div>
</div>
</div>
</text>);
})
.HtmlAttributes(new { #class = "table-responsive" })
.Groupable()
.Pageable()
.Sortable()
.Editable(editable => editable.Mode(GridEditMode.InLine))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("OB2_ClientProfiles", "OB"))
.PageSize(5)
.Update(update => update.Action("EditingInline_Update", "OB"))
.Destroy(update => update.Action("EditingInline_Destroy", "OB"))
.Model(model => model.Id(obcs => obcs.SetupID))
)
)
The Model
public partial class OBClientSetup
{
private int _setupID;
public virtual int SetupID
{
get
{
return this._setupID;
}
set
{
this._setupID = value;
}
}
The idea is so they can click a button and be sent to OBClientSetupTasks/Index/1 or whatever their ID is

To use the Grid's model that way you'll probably need something like this:
Create a JavaScript function to handle the click event. Here you can grab the SetupID. I don't know what you're controller's action is going, but there are a few options.
If you want to change to the Index page, do something like this:
<script>
function SetupTasks(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
window.location.href = '#Url.Action("Index", "OBClientSetup")' + "/" + dataItem.SetupID;
}
</script>
Or, execute some ajax request like this:
<script>
function SetupTasks(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
windows.location.href = '#Url.Action("Index", "OBClientSetup")',
type: "POST",
dataType: "json",
data: { SetupID: dataItem.SetupID },
success: function (data) {
// Handle return value
}
});
}
</script>
In your grid specify your command button like this:
command.Custom("Tasks").Click("SetupTasks");

Related

Telerik Grid: Handle click event on thick button within custom template opened on edit command

I am using a Telerik grid in an ASP.NET MVC view. I am completely new in Telerik.
My Telerik grid is as below:
#(Html.Telerik().Grid<MyDTOs.DetalleDTO>()
.Name("GridActividadesOferta")
.DataKeys(keys =>
{
keys.Add(d => d.indiceInterno).RouteKey("indiceInterno");
})
.Columns(columns =>
{
columns.Bound(cl => cl.nombreActividad).Title(Inspecciones.lblGridActividad.ToUpper()).ReadOnly(true).HtmlAttributes(new { #class = "actividad-column" });
columns.Bound(cl => cl.nombreSubActividad).Title(Inspecciones.lblGridSubActividad.ToUpper()).ReadOnly(true);
columns.Bound(cl => cl.articuloId).Title(Inspecciones.lblGridArticulo.ToUpper()).ReadOnly(true);
columns.Bound(cl => cl.CCAA_Expediente).Title("Expediente".ToUpper()).ReadOnly(false).Width(100);
columns.Bound(cl => cl.CCAA_Instalacion).Title("Instalación".ToUpper()).ReadOnly(false).Width(100);
columns.Bound(cl => cl.importeArticulo).Title("Importe".ToUpper()).ReadOnly(false).Width(100).Format("{0:c}").ClientFooterTemplate("<#= $.telerik.formatString('Total factura') #>");
columns.Bound(cl => cl.importeTasa).Title(Resources.Inspecciones.lblGridImporteTasa.ToUpper()).ReadOnly(false).Aggregate(aggr => aggr.Sum().Count())
.Width(100)
.Format("{0:c}")
//.ClientFooterTemplate("<#= $.telerik.formatString('{0:c}'," + Model.totalFactura + ")#>")
.ClientFooterTemplate("<#= '<span id=spanTotalFactura>' + $.telerik.formatString('{0:c}'," + Model.totalFactura + ") + '</span>' #>")
.Width(100)
.Format("{0:c}");
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Image);
commands.Delete().ButtonType(GridButtonType.Image);
}).Width(100).Visible(!ViewBag.readOnly);
})
.DataBinding(dataBinding =>
{
dataBinding.Ajax()
.Select("SelectGridVacias1", "Expedientes", new { ofertaComercialId = #Model.ofertaComercialId })
.Update("UpdateGridActividadesRegularizables1", "Expedientes", new { ofertaComercialId = #Model.ofertaComercialId })
.Delete("DeleteGridActividadesRegularizables1", "Expedientes", new { ofertaComercialId = #Model.ofertaComercialId })
;
})
.ClientEvents(events => events.OnDataBinding("onDataBindingArticulosGrid").OnDataBound("onDataBoundWrapper").OnEdit("onEditDatosArticulo")
.OnRowDataBound("onRowDataBound").OnDelete("onDelete").OnError("onErrorGrid"))
.HtmlAttributes(new { #class = "center-columns" })
.Selectable()
.Editable(editing =>
{
editing
.Mode(GridEditMode.PopUp)
.TemplateName("DatosArticuloTemplate1")
.InsertRowPosition(GridInsertRowPosition.Top);
})
)
As you can see above, I have two commands defined, Edit and Delete. The edit command button has a custom template specified in ".Editable". This template is called "DatosArticuloTemplate1" and it is as follows:
<div style="width:800px">
<div class="row-fluid">
<div class="span12">
#Html.Label("nombreArticulo", "Descripcion actual".ToUpper())
#Html.TextAreaFor(m => m.nombreArticuloFacturacion, new { style = string.Format("width:{0}px; ", 800) })
#Html.ValidationMessage("instalacionId")
</div>
</div>
<div class="row-fluid">
<div class="span2">
#Html.Label("importeArticulo", "Importe".ToUpper())
#(Html.Telerik().NumericTextBoxFor(model => model.importeArticulo).EmptyMessage("Importe").DecimalDigits(2).InputHtmlAttributes(new { style = "width:90px;" }))
</div>
<div class="span3">
#Html.Label("importeTasa", "Importe de tasa".ToUpper())
#(Html.Telerik().NumericTextBoxFor(model => model.importeTasa).EmptyMessage("Importe").DecimalDigits(2).InputHtmlAttributes(new { style = "width:90px;" }).Value(0))
</div>
<div class="span7">
#Html.Label("unidades", "Instalación".ToUpper())
#{Html.RenderAction("DropDownListInstalaciones", "SeleccionarInstalacionButton",
new
{
area = "",
clienteId = Model.OfertaComercial == null ? "" : Model.OfertaComercial.clienteId,
actividadId = Model.actividadId,
subActividadId = Model.subActividadId,
sociedadId = Model.OfertaComercial == null ? "" : Model.OfertaComercial.sociedadId,
dataBindTo = "instalacionId"
});
}
</div>
</div>
</div>
This custom template looks like this:
As you can see at the bottom left corner of the custom template, there are two buttons, one to validate the values introduced (thick button) and another to cancel. These buttons are from Telerik.
Now, I am trying to handle the click event on the thick button. Within the click event i want to validate the value introduced for "IMPORTE" field. If value introduced for this field is 0 then I want to popup a javascript alert modal window. This alert window must notify to the user that a value greater than 0 must be entered for that field. Also, custom template window "DatosArticuloTemplate1" must not be closed until user types in a correct value (value > 0) in the "IMPORTE" field. So how can I do it?

How to add Checked Column in Kendo Grid

#model IEnumerable<Pardon.Models.ViewModel.StudendsShowCreatAddViewModel>
<h2>#ViewBag.Title</h2>
#(Html.Kendo().Grid(Model)
.Name("grid")
.Columns(columns =>
{
columns.Bound(model => model.ISSelected).Template(#<text></text>).ClientTemplate("<input type='checkbox' #= ISSelected ? checked='checked':'' # class='chkbx' />");
//columns.Bound(model => model.ISSelected)///Bound(model => model.ISSelected)
//.ClientTemplate("<input type='checkbox' #= ISSelected ? checked='checked' : '' # disabled='enabled' ></input>");
columns.Bound(model => model.CoursesSystem_ID).Visible (false);
columns.Bound(model => model.per_Name);
columns.Bound(model => model.per_Family);
columns.Bound(model => model.stu_ID).Visible (false);
})
.ToolBar(toolbar =>
{
toolbar.Custom().Action("CreateStudents", "CoursesSystem", new {_StudendsShowCreatAddViewModel = #Model }).Text("ثبت");
}
)
.Groupable()
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Server()
)
)
<script>
$(function() {
$('#grid').on('click', '.chkbx', function() {
var checked = $(this).is(':checked');
var grid = $('#grid').data().kendoGrid;
var dataItem = grid.dataItem($(this).closest('tr'));
dataItem.set('ISSelected', checked);
});
});
</script>
I tried the above column properties Boolean ==>Isselected to checked Column and Editable and it didn't work.
For example, such as Photo
You're trying to add a client template showing a checkbox. I take a slightly different approach setting CSS classes so that when the row is not being edited I'll show a tick or cross depending upon the underlying value, then when the cell is clicked to start editing the checkbox is displayed. Optionally you can add additional CSS so the tick is coloured green and the cross red.
columns.Bound(a => a.ISSelected)
.ClientTemplate("<i class='fa fa-lg #: ISSelected ? 'fa-check' : 'fa-times' #'></i>")
.HtmlAttributes(new { #class = "text-center" })
.Title("Is Selected");
The above is using Font Awesome classes by the way.

Get Checked Rows[Selected Rows] from kendo Grid

Here is My grid, When Checkbox is Selected I want to pass the CheckBox Checked/Not Checked Value to Controller. When accessing in the controller model is not persisting the Checkbox value. Can you please guide me acheiving it.
My View :
#using(#Html.BeginForm("RequisitionApply", "Requisition", FormMethod.Post, new { approvalReason = Model, #id = "addExpenseItem" }))
{
#(Html.Kendo().Grid<ZCW.MVC.ViewModels.ManagerRequisitionSearchListViewModel>()
.Name("ManagerRequisitionApprovalGrid")
.DataSource(dataSource => dataSource
.Ajax()
.Events(e => e.RequestEnd("GetPaginationDisplay"))
.PageSize(25)
.Read(read => read.Action("GetDetails", "List").Type(HttpVerbs.Get))
//.Update(update => update.Action("Apply", "List").Type(HttpVerbs.Post))
.ServerOperation(true)
)
.Columns(columns =>
{
//RequistionName(ID)
columns.Bound(p => p.FirstName).ClientTemplate("#=FirstName#<a href='" + Url.Action("GetResource", "List", new { NameID = "#=NameID#"}) + "'>(#=NameID#) </a>").Title(#Html.LocalizedText("ID")).HtmlAttributes(new { #class = "alignleft" });
//Location
columns.Bound(p => p.Location)
.Title(#Html.LocalizedText("Location")).HtmlAttributes(new { #class = "alignleft" });
// Accept Header
columns.Template(p => p.Accept).Title("")
.HeaderTemplate("<i class='icon-ok gicn tooltips' data-original-title='Approve' data-placement='top'></i><br><div class='checker'><span><input id='selectallApprove' class='chkboxApprove' type='checkbox' onclick='ToggleChkBoxApprove(this.checked);' #= Accept ? checked='checked' : '' # /></span></div>")
.ClientTemplate("<span> <input id='checkbox' onclick='grdChkBoxClick(this); ' class='chkboxApprove' type='checkbox' name='remember' /></span>")
.HeaderHtmlAttributes(new { #class = "aligncenter" })
.HtmlAttributes(new { #class = "textaligncenter" });
//Decline Header
columns.Template(p => p.Decline).Title("")
.HeaderTemplate("<i class='icon-remove ricn tooltips' data-original-title='Decline' data-placement='top'></i><br><div class='checker'><span> <input id='selectallDecline' class='chkboxDecline' type='checkbox' onclick='ToggleChkBoxDecline(this.checked);' #= Decline ? checked='checked' : '' # /></span></div>")
.ClientTemplate("<span> <input id='checkbox1' onclick='grdChkBoxClick(this); ' class='chkboxDecline' type='checkbox' name='remember'/></span>")
.HeaderHtmlAttributes(new { #class = "aligncenter" })
.HtmlAttributes(new { #class = "textaligncenter" });
})
.AutoBind(true)
.Sortable()
.EnableCustomBinding(true)
)
<div class="pull-right">
<button type="submit" id="Apply" class="submitbtn" style="border: 0px none;"><i class="greenbtn"></i>#GlobalResourceHelper.GetGlobalResource(ResourceFiles.ButtonsResources, "BTN_APPLY")</button>
</div>
}
MyController :
public ActionResult Apply(ResourceViewModel ApprovalList)
{
// I'm not getting Checkbox Checked or not in ApprovalList
}
You have to do this.
columns.Bound(x => x.IsChecked).ClientTemplate(
"<input name='IsChecked' class='chkBox' type='checkbox'
data-bind='checked: IsChecked' #= IsChecked ? checked='checked' : '' #/>");
//check document ready
var grid = $("#Grid").data("kendoGrid");
grid.tbody.on("change", ".chkBox", function (e) {
var row = $(e.target).closest("tr");
var item = grid.dataItem(row);
//item.set("IsChecked", $(e.target).is(":checked") ? 1 : 0);
kendo.bind(this,item);//this line or above line will handle this
});
Post the Grid using an ajax call and send the Grid's data.
var gridData = $("#Grid").data("kendoGrid");
//AJAX etc...
data: JSON.stringify(gridData.dataSource.view()),

Post the Kendo Grid Data on Form Submit

I want to Post the data from a Kendo Grid to the server, and save it to a database.
For this I have used form like so:
#using (Html.BeginForm("MainDocumentSave","Document"))
{
<div class="row-fluid">
<div class="span10">
#(Html.Kendo().Grid<Invoice.Models.ViewModels.SegmentViewModel>()
.Name("Segment")
.TableHtmlAttributes(new { style = "height:20px; " })
.Columns(columns =>
{
columns.Bound(p => p.AirlineShortName).EditorTemplateName("AirlineEditor").Title("Airline").ClientTemplate("#=AirlineName#").Width(5);
columns.Bound(p => p.DepartureDate).Width(9);
columns.Bound(p => p.Arrives).EditorTemplateName("ArrivalLocation").Title("Arrival").ClientTemplate("#=Arrives#").Width(5);
columns.Bound(p => p.ArrivalDate).Width(7);
columns.Bound(p => p.FlightNumber).Width(8);
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Navigatable()
.Sortable()
.Scrollable(scr => scr.Height(200))
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.AirlineName))
.Create("Editing_Create", "Grid")
.Read("Segment_Read", "Document")
.Update("Editing_Update", "Grid")
.Destroy("Editing_Destroy", "Grid")
)
)
</div>
</div>
<button type="submit" class="btn btn-primary"> Save Segments</button>
}
But after submitting, the data inside the Kendo Grid is not Posted. How can I Post Kendo Grid Data to the Server?
The Grid is not a form element and it cannot be simply posted to the server. You can take advantage of the templates that the Grid provide and create hidden elements based on the different row models which to be submitted to the server.
The same approach is used in this code library which demonstrates exactly what you are searching for.
The grid data isn't in form elements. The form elements appear only when a cell is being edited, then it is removed. You can't post the data to the server by using a form submit button.
The proper way to to this would be by adding the 'save' command button that the grid provides itself:
#(Html.Kendo().Grid<Invoice.Models.ViewModels.SegmentViewModel>()
.Name("Segment")
.ToolBar(toolbar => {
toolbar.Save(); // add save button to grid toolbar
})
// ... rest of options ...
Or by calling saveChanges() on the Grid widget:
<button type="button" id="save">Save Segments</button>
$("#save").on("click", function () {
$("#Segment").data("kendoGrid").saveChanges();
});
Suppose I have three kendo grids and two textboxs in my page. Now I want to post all data along with data of three grid. I did this by below style.
#model DAL.ViewModel.ProfileViewModel
#{
ViewBag.Title = "Profile";
Layout = "~/Views/Shared/_LayoutAfterLogin.cshtml";
}
<h2>Profile</h2>
<div>
<h4>ApplicationUser</h4>
<hr />
<dl class="dl-horizontal"></dl>
<form id="frmProfile">
<div>
<label>Email<span class="mandatory"></span></label>
#Html.Kendo().TextBoxFor(model => model.Email)
</div>
<div>
<label>UserName<span class="mandatory"></span></label>
#Html.Kendo().TextBoxFor(model => model.UserName)
</div>
</form>
#(Html.Kendo().Grid<DAL.ViewModel.PhoneViewModel>()
.Name("PhoneGrid")
.Columns(columns =>
{
columns.Bound(p => p.PhoneID).Groupable(false);
columns.Bound(p => p.PhoneType).Width(160);
columns.Bound(p => p.PhoneNumber).Width(120);
columns.Bound(p => p.IsPrimary).Width(120);
columns.Command(command => command.Destroy()).Width(90);
})
.ToolBar(toolBar =>
{
toolBar.Create();
// toolBar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(p => p.PhoneID);
model.Field(p => p.PhoneID).Editable(false);
})
.PageSize(20)
.Read(read => read.Action("PhoneList", "Account"))
.Create(create => create.Action("AddPhone", "Account"))
.Update(update => update.Action("EditPhone", "Account"))
.Destroy(destroy => destroy.Action("DeletePhone", "Account"))
)
)
</div>
<p>
<button type="button" id="btnSave">Save</button>
#Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
#Html.ActionLink("Back to List", "Index")
</p>
//jquery
$("#btnSave").on("click", function () {
sendData();
});
function sendData() {
var grid = $("#PhoneGrid").data("kendoGrid"),
parameterMap = grid.dataSource.transport.parameterMap;
//get the new and the updated records
var currentData = grid.dataSource.data();
var updatedRecords = [];
var newRecords = [];
for (var i = 0; i < currentData.length; i++) {
if (currentData[i].isNew()) {
//this record is new
newRecords.push(currentData[i].toJSON());
} else if (currentData[i].dirty) {
updatedRecords.push(currentData[i].toJSON());
}
}
//this records are deleted
var deletedRecords = [];
for (var i = 0; i < grid.dataSource._destroyed.length; i++) {
deletedRecords.push(grid.dataSource._destroyed[i].toJSON());
}
var serializedData = $("#frmProfile").serializeObject();
var data = {};
$.extend(data, parameterMap({ updated: updatedRecords }), parameterMap({ deleted: deletedRecords }), parameterMap({ new: newRecords }));
var finaldata = {};
$.extend(finaldata, parameterMap({ phone: data }), parameterMap({ email: data }), parameterMap({ address: data }), parameterMap({ pagedata: serializedData }));
$.ajax({
url: '#Url.Action("UpdateCreateDelete1", "Account")',
data: JSON.stringify(finaldata),
type: "POST",
contentType: 'application/json',
dataType: 'json',
error: function (e) {
alert('error');
//Handle the server errors using the approach from the previous example
},
success: function () {
grid.dataSource._destroyed = [];
//refresh the grid - optional
// grid.dataSource.read();
}
})
}
jQuery.fn.serializeObject = function () {
var arrayData, objectData;
arrayData = this.serializeArray();
objectData = {};
$.each(arrayData, function () {
var value;
if (this.value != null) {
value = this.value;
} else {
value = '';
}
if (objectData[this.name] != null) {
if (!objectData[this.name].push) {
objectData[this.name] = [objectData[this.name]];
}
objectData[this.name].push(value);
} else {
objectData[this.name] = value;
}
});
return objectData;
};
//action method
public ActionResult UpdateCreateDelete1(
[Bind(Prefix = "phone.updated")]List<PhoneViewModel> updatedPhone,
[Bind(Prefix = "phone.new")]List<PhoneViewModel> newPhone,
[Bind(Prefix = "phone.deleted")]List<PhoneViewModel> deletedPhone,
[Bind(Prefix = "email")]List<PhoneViewModel> emaillist,
[Bind(Prefix = "address")]List<PhoneViewModel> addresslist,
[Bind(Prefix = "pagedata")] ProfileViewModel pagedata
)
{
}

Why Is `data(“kendogrid”)` Undefined?

I'm a starter in kendo.ui, I've written this code to create kendo.ui.grid
#(Html.Kendo().Grid<BrandViewModel>(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.BrandName);
columns.Bound(p => p.BrandAbbr);
columns.Bound(p => p.SrcImage);
columns.Command(command => command.Custom("Edit").Click("editItem"));
})
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("CustomCommand_Read", "Brand"))
.Model(model => model.Id(p => p.Id))
)
)
When the user clicks the edit button in grid it will show Edit view in kendo.ui.window and the user can edit data.
#(Html.Kendo().Window().Name("Details")
.Title("Customer Details")
.Visible(false)
.Modal(true)
.Height(400)
.Draggable(true)
.Width(300)
.Events(events => events.Close("onClose"))
)
<script type="text/x-kendo-template" id="template">
<div id="details-container">
<!-- this will be the content of the popup -->
BrandName: <input type='text' value='#= BrandName #' />
</div>
</script>
<script type="text/javascript">
var detailsTemplate = kendo.template($("#template").html());
var windowObject;
$(document).ready(function () {
windowObject = $("#Details").data("kendoWindow");
});
function editItem(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
windowObject.refresh({
url: "/Brand/Edit/" + dataItem.Id
});
windowObject.center().open();
}
function onClose(e) {
var grid = $("#Grid").data("kendoGrid").dataSource.read();
}
</script>
but in onClose method $("#Grid").data("kendoGrid") is Undefined, please help me, thanks all
try Window load event
$(window).load(function () {
var grid = $("#grid").data("kendoGrid");
this work for me.
var grid = $("#Grid").data("kendoGrid"); //call grid by Name of Grid you used in razor

Resources