I am using a Kendo MVC Grid and i want to have a dropdownlist in one of the cells.
here is my code:
#(Html.Kendo().Grid<RMS.Admin.ViewModel>()
.Name("ResourceGrid")
.Columns(columns =>
{
columns.Bound(c => c.ResourceName);
columns.Bound(c => c.Descritption);
columns.Bound(c => c.ResourceType).ClientTemplate("#=ResourceType#");
columns.Bound(c => c.Approved);
columns.Bound(c => c.IsEnabled);
columns.Bound(c => c.Data).Width(220);
columns.Command(command =>
{
command.Edit();
command.Destroy();
}).Width(172).Title("Edit/Delete");
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Scrollable()
.Sortable()
.HtmlAttributes(new { style = "height:800px" })
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.ClientDetailTemplateId("template")
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(s => s.ResourceId);
model.Field(p => p.ResourceType).DefaultValue(ViewData["defResourceType"] as RMS.Admin.ViewModel.ResourceTypeId);
})
.Create(update => update.Action("CreateResource", "Home", new { resourceTypeId = "#=ResourceType.Id#" }))
.Read(read => read.Action("ReadResource", "Home"))
.Update(update => update.Action("UpdateResource", "Home"))
.Destroy(destroy => destroy.Action("RemoveResource", "Home"))
)
.Events(events => events.DataBound("dataBound"))
)
The problem is that i don't know what clienttemplate is so i don't know what to do with it.
If bound resourcetype with a clienttemplate i can't add new records to the grid, i get the error: Uncaught ReferenceError: ResourceType is not defined
If i remove clienttemplate i can add a record but when i try to save it it says it can't find the id of ResourceType.
basically if you want to use a dropdownlist within a kendo ui grid you have to define an editor template which includes a kendo dropdownlist widget. this means you have to declare the respective cell with the attribute .EditorTemplateName() like this:
.Columns(columns =>
{
columns.Bound(c => c.ResourceName);
columns.Bound(c => c.Descritption);
columns.Bound(c => c.ResourceType).EditorTemplateName("TemplateName");
columns.Bound(c => c.Approved);
columns.Bound(c => c.IsEnabled);
columns.Bound(c => c.Data).Width(220);
columns.Command(command =>
{
command.Edit();
command.Destroy();
}).Width(172).Title("Edit/Delete");
})
the editortemplate has to be stored within folder: Shared/EditorTemplates/TemplateName.cshtml
TemplateName.cshtml should look like this:
#model ModelName
#(Html.Kendo().DropDownList()
.OptionLabel("please select a value")
.Name("ResourceType") <-- the name needs to be equal to the grid cell name
.DataSource(source=>
{
source.Read( read =>
{
read.Action("ActionName", "ControllerName");
})
.ServerFiltering(true);
})
.DataTextField("Name") <-- displays the text of the object
.DataValueField("ID") <-- stores the id of the object
)
hope this will lead you into the right direction.
cheers,
Related
I have a Hierarchical grid and I'm trying to add a custom editor for pop-up editing. When I add the template to the child grid and click on the "edit" button, i get a invalid template error. If i add that same template to the parent, it works fine.
Here is the error in console:
Uncaught Error: Invalid template:'
Here is the code
#(Html.Kendo().Grid<ParentViewModel>()
.Name("GridAdjax")
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Read(read => read.Action("Read", "controller"))
.Model(model =>
{
model.Id(c => c.Id);
})
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Events(events => events.RequestEnd("onRequestEnd"))
)
.Columns(columns =>
{
columns.Bound(p => p.CompanyName).Title("Company Name");
columns.Bound(p => p.CompanyDomain).Title("Company Domain");
columns.Bound(p => p.CompanySecurityRole).Title("Security Role");
columns.Bound(p => p.CompanySecurityGroup).Title("Security Group");
})
.ClientDetailTemplateId("template")
.Pageable()
.Sortable()
.Resizable(resize => resize.Columns(true))
.Events(e => e.DataBound("OnDataBound"))
.Deferred()
)
<CHILD>
<script id="template" type="text/kendo-tmpl">
#(Html.Kendo().Grid<ChildlViewModel>()
.Name("grid_#=CompanyId#")
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Update(update => update.Action("CompanyList_Update", "Administration"))
.Read(read => read.Action("Read", "Child", new { companyId = "#=CompanyId#" }))
.Model(model =>
{
model.Id(c => c.Id);
model.Field(c => c.CompanySoldTo).Editable(true);
model.Field(c => c.CompanyDistributionChannel).Editable(true);
model.Field(c => c.CompanyDivision).Editable(true);
model.Field(c => c.CompanyPlant).Editable(true);
model.Field(c => c.CompanySalesOrg).Editable(true);
})
)
.Columns(columns =>
{
columns.Bound(p => p.CompanySoldTo).Title("Sold To");
columns.Bound(p => p.CompanyDistributionChannel).Title("Dist. Chan.");
columns.Bound(p => p.CompanyPlant).Title("Plant");
columns.Bound(p => p.CompanySalesOrg).Title("Sales Org");
columns.Command(command => { command.Edit(); }).Title(("Edit SAP Info."));
})
.Editable(ed => ed.Mode(GridEditMode.PopUp).TemplateName("Detail"))
.Pageable()
.Sortable()
.ToClientTemplate()
)
</script>
<script>
function dataBound() {
this.expandRow(this.tbody.find("tr.k-master-row").first());
}
</script>
If anyone experiences this issue in the future, the problem was actually in my custom pop-up editor template. I had a data validation message on one of the input boxes set as "Please enter your account #". The "#" was being interpreted as a template by Kendo. I escaped the # and then was able to get it to successfully load.
I'm using the Kendo Grid control to display some data and it's working OK. This is the code:
#(Html.Kendo().Grid<MyModel>()
.Name("MyGrid")
.ToolBar(toolbar =>
{
toolbar.Create().Text("Add New Line").HtmlAttributes(new { #Class = "btn btn-primary" });
})
.Columns(columns =>
{
columns.Bound(p => p.Description).HtmlAttributes(new {style = "font-size: 9pt;"});
columns.Bound(p => p.Value);
columns.Command(cmd =>
{
cmd.Edit();
cmd.Destroy();
}).Width(200);
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Selectable(s => s.Mode(GridSelectionMode.Multiple).Type(GridSelectionType.Row))
.DataSource(data => data
.Ajax()
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.Id))
.ServerOperation(false)
.Read(read => read.Action("ReadAction", "MyController"))
.Create(create => create.Action("CreateAction", "MyController"))
.Update(update => update.Action("UpdateAction", "MyController"))
.Destroy(delete => delete.Action("DeleteAction", "MyController"))
))
The code above is working without any problem. Now, what I'm trying to implement is to display the same data using the MVVM pattern. I mean, I would like to have the same operations (create, update, delete) in my viewmodel without calling the server actions of my controller as the code above does.
I have google it for a while but without any luck.
Any help would be very appreciate
This is my code:
#(Html.Kendo().Grid<RMS.Admin.ViewModel>()
.Name("ResourceGrid")
.Columns(columns =>
{
columns.Bound(c => c.ResourceName);
columns.Bound(c => c.Descritption);
columns.Bound(c => c.ResourceType).ClientTemplate("#=ResourceType.Name#");
columns.Bound(c => c.Approved);
columns.Bound(c => c.IsEnabled);
columns.Bound(c => c.Data).Width(220);
columns.Command(command =>
{
command.Edit();
command.Destroy();
}).Width(172).Title("Edit/Delete");
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Scrollable()
.Sortable()
.HtmlAttributes(new { style = "height:800px" })
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.ClientDetailTemplateId("template")
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(s => s.ResourceId);
model.Field(p => p.ResourceType).DefaultValue(ViewData["defResourceType"] as RMS.Admin.ViewModel.ResourceTypeId);
})
.Create(update => update.Action("CreateResource", "Home", new { resourceTypeId = "#=ResourceType.Id#" }))
.Read(read => read.Action("ReadResource", "Home"))
.Update(update => update.Action("SaveSystem", "Home"))
.Destroy(destroy => destroy.Action("RemoveSystem", "Home"))
)
.Events(events => events.DataBound("dataBound"))
)
<script id="template" type="text/kendo-tmpl">
#(Html.Kendo().Grid<RMS.Admin.Metadata>()
.Name("grid_#=ResourceId#")
.Columns(columns =>
{
columns.Bound(o => o.MetaDataName);
columns.Bound(o => o.MetaDataDescription);
columns.Bound(o => o.LanguageCode);
columns.Command(command =>
{
command.Edit();
command.Destroy();
}).Width(172).Title("Edit/Delete");
})
.HtmlAttributes(new { style = "height:500px" })
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Model(model => model.Id(s => s.MetaDataId))
.Read(read => read.Action("GetMetaDataByResourceId", "Home", new { id = "#=ResourceId#" }))
.Create(update => update.Action("CreateMetaData", "Home", new { id = "#=ResourceId#" }))
.Update(update => update.Action("SaveSystem", "Home"))
.Destroy(destroy => destroy.Action("RemoveSystem", "Home"))
)
.Scrollable()
.Sortable()
.ToClientTemplate()
)
So when i add clienttemplate to resourcetype on the main grid everything works as it should but when i press create button is tells me
"Uncaught ReferenceError: ResourceType is not defined "
If i take clienttemplate away the dropdownlist in the grid wont work.
I have been working with the Kendo UI for ASP.NET MVC suite and can't figure out how to do something that I feel is very simple.
I have a basic grid that loads customer information: Name, Phone, Fax, Website, etc.
I want the far right column to be an edit button that simply calls the Details Action Result passing it the id of the current row that it is clicked from.
I really didn't want to have to resort to asking this on here, but all of the examples deal with inline editing and popup editing.
Below is my current code:
#(Html.Kendo().Grid<CustomerViewModel>()
.Name("CustomerGrid")
.Columns(columns => {
columns.Bound(c => c.Name);
columns.Bound(c => c.Phone);
columns.Bound(c => c.Fax);
columns.Bound(c => c.Website);
})
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Customers_Read", "Customer"))
))
Solution
#(Html.Kendo().Grid<CustomerViewModel>()
.Name("CustomerGrid")
.Columns(columns => {
columns.Bound(c => c.Name).ClientTemplate("" + #Html.ActionLink("#: Name#", "ViewCompanyDetails", "Company", new {companyId = "#: CompanyId#"}, new {Title = "View #: CompanyName#"}) + "");
columns.Bound(c => c.Phone);
columns.Bound(c => c.Fax);
columns.Bound(c => c.Website);
})
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Customers_Read", "Customer"))
))
Solution
#(Html.Kendo().Grid<CustomerViewModel>()
.Name("CustomerGrid")
.Columns(columns => {
columns.Bound(c => c.Phone);
columns.Bound(c => c.Fax);
columns.Bound(c => c.Website);
columns.Command(command => command.Custom("ViewDetails").Click("location.href='#Url.Action("ViewCustomerDetail", "Customer", new { CustomerId = #=CustomerId# })'"));
})
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Customers_Read", "Customer"))
))
I am working on Kendo UI with asp.net mvc razor. I am trying to bind database table data with kendo grid that supports CRUD operations. Here i need to populate a dropdownlist for one of my table field. I have used the following code
View:
#model IEnumerable<MvcApplication1.PriceOption>
#(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
//columns.Bound(p => p.ProductTitle).ClientTemplate("<input type='checkbox' disabled='disabled'name='Discontinued' <#= Discontinued? checked='checked' : '' #> />");
columns.Bound(p => p.ProductTitle).EditorTemplateName("OptionalEmail");
columns.Bound(p => p.OptionTitle);
columns.Bound(p => p.Price);
columns.Bound(p => p.Frequency);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.ProductID))
.Create(create => create.Action("CreateOption", "ZiceAdmin"))
.Read(read => read.Action("Read", "ZiceAdmin"))
.Update(update => update.Action("UpdateOption", "ZiceAdmin"))
.Destroy(update => update.Action("DeleteOption", "ZiceAdmin"))
)
)
OptionalEmail.cshtml
#model string
#(Html.Kendo().DropDownList()
.Name("ProductTitle")
.Value(Model)
.SelectedIndex(0)
.BindTo(new SelectList(ViewBag.ProductTitle))
)
Here i need to store the selected item from the dropdownlist. But it always shows null. How could i get the selected value from dropdownlist.
It's a bug. Remove the name, and it will submit back to the server as part of the ViewModel:
#model string
#(Html.Kendo().DropDownList()
.Value(Model)
.SelectedIndex(0)
.BindTo(new SelectList(ViewBag.ProductTitle))
)