DEVEXTREME Save all selected data in grid inside edit form to database - razor

multiple select item in Edit form with grid
How to save the data included the selected item at grid using DEVEXTREME.
The condition now is i can show the grid but now i struggle on get all the data inside the grid to save it and bring it to database, including need to show it again when the user want to edit the data
Template:
#using (Html.DevExtreme().NamedTemplate("EmbeddedDataGridMultiple"))
{
#(Html.DevExtreme().DataGrid()
.ID("PrincipalGrid")
.DataSource(d => d.WebApi().Controller("MasterPrincipal").LoadAction("GetPrincipalData").Key("PrincipalId"))
.Columns(columns => {
columns.Add().DataField("Principal");
columns.Add().DataField("Description");
})
.HoverStateEnabled(true)
.Paging(p => p.PageSize(10))
.FilterRow(f => f.Visible(true))
.Scrolling(s => s.Mode(GridScrollingMode.Virtual))
.Height(345)
.Selection(s => s.Mode(SelectionMode.Multiple))
.SelectedRowKeys(new JS(#"component.option(""value"")"))
.OnSelectionChanged(#<text>
function(selectedItems) {
var keys = selectedItems.selectedRowKeys;
component.option("value", keys);
}
</text>)
)
}
Form
#(Html.DevExtreme().DataGrid<MasterGroupQuotaValidateModel>
()
.ID("gridData")
.ShowBorders(true)
.DataSource(d => d.Mvc().Controller("MasterGroupQuota")
.LoadAction("GetGroupQuota")
.Key("IdGroup")
.UpdateAction("UpdateMasterGroupQuota")
.DeleteAction("DeleteMasterGroupQuota")
.InsertAction("InsertMasterGroupQuota")
)
.RemoteOperations(true)
.Columns(columns =>
{
columns.AddFor(m => m.RowNumber).Caption("No").Width(70);
columns.AddFor(m => m.GroupQuotaId).Visible(false);
columns.AddFor(m => m.SitePlan)
.Lookup(lookup => lookup
.DataSource(d => d.WebApi().Controller("MasterSite").LoadAction("GetSitePlan").Key("SiteId"))
.DisplayExpr("SitePlan")
.ValueExpr("SiteId")
).ValidationRules(rules =>
{
rules.AddAsync().ValidationCallback("CheckGroupQuota").Message("This Site is already registered with the follwoing GroupQuota and Principal");
}); ;
columns.AddFor(m => m.Description).Caption("Group Quota").ValidationRules(rules =>
{
rules.AddAsync().ValidationCallback("CheckGroupQuota").Message("This Group Quota is already registered with the follwoing Site Plan and Principal");
});
columns.AddFor(m => m.Principal)
.Lookup(lookup => lookup
.DataSource(d => d.WebApi().Controller("MasterPrincipal").LoadAction("GetPrincipalData").Key("PrincipalId"))
.DisplayExpr("Principal")
.ValueExpr("PrincipalId")
).ValidationRules(rules =>
{
rules.AddAsync().ValidationCallback("CheckGroupQuota").Message("This Principal is already registered with the follwoing GroupQuota and Site Plan");
});
})
.Paging(paging => paging.PageSize(5))
.Pager(pager =>
{
pager.Visible(true);
pager.ShowPageSizeSelector(true);
pager.AllowedPageSizes(new JS("[5, 10, 'all']"));
pager.ShowInfo(true);
pager.ShowNavigationButtons(true);
})
.Sorting(sorting=> sorting.Mode(GridSortingMode.Single))
.FilterRow(f => f.Visible(true))
.HeaderFilter(f => f.Visible(true))
.AllowColumnReordering(true)
.ShowBorders(true)
.Grouping(grouping => grouping.AutoExpandAll(true))
.SearchPanel(searchPanel => searchPanel.Visible(true))
.GroupPanel(groupPanel => groupPanel.Visible(true))
.HoverStateEnabled(true)
.ShowRowLines(true)
.RowAlternationEnabled(true)
.Scrolling(scrolling => scrolling.RowRenderingMode(GridRowRenderingMode.Virtual))
.Selection(s => s.Mode(SelectionMode.Single))
.OnSelectionChanged("gridSelectionChanged")
.OnExporting("exporting")
.OnRowUpdating("RowUpdating")
.OnEditorPreparing("onEditorPreparing")
.OnSaved("setItemTreeToTextSaving")
.OnEditingStart(#<text>
function(e) {
GetKeyId(e.key)
}
</text>)
.Editing(e => e.Mode(GridEditMode.Popup)
.AllowUpdating(false)
.AllowAdding(false)
.AllowDeleting(false)
.UseIcons(true)
.Popup(p => p
.ShowTitle(true)
.Width(700)
.Height(525)
)
.Form(f => f.Items(items =>
{
items.AddGroup()
.Caption("Detail Group Quota")
.ColCount(2)
.ColSpan(2)
.Items(groupItems =>
{
groupItems.AddSimpleFor(m => m.GroupQuotaId).Visible(false);
groupItems.AddSimpleFor(m => m.SitePlan);
groupItems.AddSimpleFor(m => m.Description);
});
items.AddGroup()
.Caption("Principal")
.ColCount(2)
.ColSpan(2)
.Items(groupItems =>
{
groupItems.AddSimpleFor(m => m.Principal).Template(new TemplateName("EmbeddedDataGridMultiple")).ColSpan(2);
});
}))
)
)

Related

writing a script/Function for checkboxes and Custom Delete button in Telerik UI for ASP.NET MVC

I need help writing a script/Function for my Telerik UI for asp.net MVC program. I have a delete button in my tool bar and I think my script is correct for it deleting. Now, I'm told that my check boxes haft to have a script also to be deleted when checked. As a C# coder, I'm not entirely knowledgeable about Json coding. So, any help would be appreciated! Here is my code below.
#(Html.Kendo().Grid<MVCSQLDatabase.Models>()
.Name("Grid")
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Batch(true)
.Model(model => model.Id(p => p.Proposal_Uid))
.Read(read => read.Action("Proposals_Read", "Grid"))
.Create(create => create.Action("Proposals_Create", "Grid"))
.Update(update => update.Action("Proposals_Update", "Grid"))
.Destroy(destroy => destroy.Action("Proposals_Destroy", "Grid"))
)
.Resizable(resize => resize.Columns(true))
.Columns(columns =>
{
columns.Select().Width(100); //<-- my check boxes code.
columns.Bound(c => c.Prime).Width(215);
columns.Bound(c => c.Proposal).Width(200);
columns.Bound(c => c.C).Width(190);
columns.Bound(c => c.Cl).Width(185);
columns.Bound(c => c.T).Width(290);
columns.Bound(c => c.M).Width(220);
columns.Bound(c => c.S).Format("{0: dd/MM/yyyy}").Width(170);
columns.Bound(c => c.E).Format("{0: dd/MM/yyyy}").Width(170);
columns.Bound(c => c.P).Width(235);
columns.Bound(c => c.Con).Width(215);
columns.Command(command => { command.Destroy(); }).Width(180);// <--- My delete button in my column
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
toolbar.Excel();
toolbar.Custom().Text("Delete").Name("batchDestroy").IconClass("k-icon k-i-close"); //<-- my custom made delete button in my toolbar.
})
.ColumnMenu()
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Selectable(selectable =>
{
selectable.Mode(GridSelectionMode.Multiple);
selectable.Type(GridSelectionType.Row);
})
.PersistSelection()
.Filterable(filterable => filterable.Mode(GridFilterMode.Row))
.Scrollable()
.HtmlAttributes(new { style = "height:835px;" })
)
<script>
$("#grid").on("click", "batchDestroy", function() {
var $tr = $(this).closest("tr"),
grid = $("#grid").data("kendoGrid"),
dataItem = grid.dataItem($tr);
grid.dataSource.remove(dataItem);
});
</script>
Here is the code for anybody else stuck in this situation. The thing that got me the most, was the .done function to end the button click function and then saving the grid AFTER the delete was made. Hopefully, this helps others!
<script>
$(document).ready(function ()
{
$(".k-grid-Destroy").on("click", function (e)
{
e.preventDefault();
var grid = $("#Grid").data("kendoGrid");
var selectedRows = grid.select();
kendo.confirm(kendo.format("Are you sure you wish to delete {0} records?", selectedRows.length))
.done(function ()
{
$.each(selectedRows, function (i, row)
{
grid.removeRow(row);
})
grid.saveChanges();
});
});
});
</script>

How to update abp db table names to MySql convention like "abp_user_role"

I'm trying to convert the ABP template project's DB table names to MySql convention like 'abp_user_role', but failed for AbpRoleBase, AbpTenantBase, AbpUserBase, others are all good.
Following are my code in DB Context:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
UpdateAbpTableNamesForMySqlConvention(modelBuilder);
modelBuilder.Entity<TestEntity>(entity =>
{
entity.HasKey(e => new { e.Id });
entity.ToTable("test_entity");
});
}
private void UpdateAbpTableNamesForMySqlConvention(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AuditLog>(e => { e.ToTable("abp_audit_log"); });
modelBuilder.Entity<BackgroundJobInfo>(e => { e.ToTable("abp_background_job"); });
modelBuilder.Entity<Edition>(e => { e.ToTable("abp_edition"); });
modelBuilder.Entity<EntityChange>(e => { e.ToTable("abp_entity_change"); });
modelBuilder.Entity<EntityChangeSet>(e => { e.ToTable("abp_entity_change_set"); });
modelBuilder.Entity<EntityPropertyChange>(e => { e.ToTable("abp_entity_property_change"); });
modelBuilder.Entity<FeatureSetting>(e => { e.ToTable("abp_feature"); });
modelBuilder.Entity<ApplicationLanguage>(e => { e.ToTable("abp_language"); });
modelBuilder.Entity<ApplicationLanguageText>(e => { e.ToTable("abp_language_text"); });
modelBuilder.Entity<NotificationInfo>(e => { e.ToTable("abp_notification"); });
modelBuilder.Entity<NotificationSubscriptionInfo>(e => { e.ToTable("abp_notification_subscription"); });
modelBuilder.Entity<OrganizationUnit>(e => e.ToTable("abp_organization_unit"));
modelBuilder.Entity<PermissionSetting>(e => e.ToTable("abp_permission_setting"));
modelBuilder.Entity<RoleClaim>(e => e.ToTable("abp_role_claim"));
//modelBuilder.Entity<AbpRoleBase>(e => e.ToTable("abp_role"));
modelBuilder.Entity<Setting>(e => e.ToTable("abp_setting"));
modelBuilder.Entity<TenantNotificationInfo>(e => e.ToTable("abp_tenant_notification"));
//modelBuilder.Entity<AbpTenantBase>(e => e.ToTable("abp_tenant"));
modelBuilder.Entity<UserAccount>(e => e.ToTable("abp_user_account"));
modelBuilder.Entity<UserClaim>(e => e.ToTable("abp_user_claim"));
modelBuilder.Entity<UserLoginAttempt>(e => e.ToTable("abp_user_login_attempt"));
modelBuilder.Entity<UserLogin>(e => e.ToTable("abp_user_login"));
modelBuilder.Entity<UserNotificationInfo>(e => e.ToTable("abp_user_notification"));
modelBuilder.Entity<UserOrganizationUnit>(e => e.ToTable("abp_user_organization_unit"));
modelBuilder.Entity<UserRole>(e => e.ToTable("abp_user_role"));
//modelBuilder.Entity<AbpUserBase>(e => e.ToTable("abp_user"));
modelBuilder.Entity<UserToken>(e => e.ToTable("abp_user_token"));
}
Got Error:
The filter expression 'e => (Not(Convert(e, ISoftDelete).IsDeleted) OrElse (Convert(e, ISoftDelete).IsDeleted != value(Test.EntityFrameworkCore.TestDbContext).IsSoftDeleteFilterEnabled))' cannot be specified for entity type 'Tenant'. A filter may only be applied to the root entity type in a hierarchy.
Correct one:
private void UpdateAbpTableNamesForMySqlConvention(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AuditLog>(e => { e.ToTable("abp_audit_log"); });
modelBuilder.Entity<BackgroundJobInfo>(e => { e.ToTable("abp_background_job"); });
modelBuilder.Entity<Edition>(e => { e.ToTable("abp_edition"); });
modelBuilder.Entity<EntityChange>(e => { e.ToTable("abp_entity_change"); });
modelBuilder.Entity<EntityChangeSet>(e => { e.ToTable("abp_entity_change_set"); });
modelBuilder.Entity<EntityPropertyChange>(e => { e.ToTable("abp_entity_property_change"); });
modelBuilder.Entity<FeatureSetting>(e => { e.ToTable("abp_feature"); });
modelBuilder.Entity<ApplicationLanguage>(e => { e.ToTable("abp_language"); });
modelBuilder.Entity<ApplicationLanguageText>(e => { e.ToTable("abp_language_text"); });
modelBuilder.Entity<NotificationInfo>(e => { e.ToTable("abp_notification"); });
modelBuilder.Entity<NotificationSubscriptionInfo>(e => { e.ToTable("abp_notification_subscription"); });
modelBuilder.Entity<OrganizationUnit>(e => e.ToTable("abp_organization_unit"));
modelBuilder.Entity<PermissionSetting>(e => e.ToTable("abp_permission_setting"));
modelBuilder.Entity<RoleClaim>(e => e.ToTable("abp_role_claim"));
modelBuilder.Entity<Role>(e => e.ToTable("abp_role"));
modelBuilder.Entity<Setting>(e => e.ToTable("abp_setting"));
modelBuilder.Entity<TenantNotificationInfo>(e => e.ToTable("abp_tenant_notification"));
modelBuilder.Entity<Tenant>(e => e.ToTable("abp_tenant"));
modelBuilder.Entity<UserAccount>(e => e.ToTable("abp_user_account"));
modelBuilder.Entity<UserClaim>(e => e.ToTable("abp_user_claim"));
modelBuilder.Entity<UserLoginAttempt>(e => e.ToTable("abp_user_login_attempt"));
modelBuilder.Entity<UserLogin>(e => e.ToTable("abp_user_login"));
modelBuilder.Entity<UserNotificationInfo>(e => e.ToTable("abp_user_notification"));
modelBuilder.Entity<UserOrganizationUnit>(e => e.ToTable("abp_user_organization_unit"));
modelBuilder.Entity<UserRole>(e => e.ToTable("abp_user_role"));
modelBuilder.Entity<User>(e => e.ToTable("abp_user"));
modelBuilder.Entity<UserToken>(e => e.ToTable("abp_user_token"));
}

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.

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, Razor braces problems

I checked braces, brackets 100 times...I think they are correctly placed but I get
"Keyword, identifier, or string expected after verbatim specifier: #"
<script id="customercontactsTemplate" type="text/kendo-tmpl">
#(Html.Kendo().TabStrip()
.Name("TabStripCustomer")
.SelectedIndex(0)
.Items(items =>
{
items.Add().Text("Contacts").Content(obj =>
#(Html.Kendo().Grid<ModelApp.Models.CustomerContacts>()
.Name("customercontacts")
.Columns(columns =>
{
columns.Bound(l => l.CustomerContactID);
columns.Bound(l => l.CustomerID);
columns.Bound(l => l.CustomerContactName);
columns.Bound(l => l.CustomerContactPhone);
columns.Bound(l => l.CustomerContactDuty);
})
.DataSource(dataSource => dataSource
.Ajax()
.Model
(model=>{
model.Id(l => l.CustomerContactID);
model.Field(l=>l.CustomerContactID).Editable(false);
model.Field(l => l.CustomerID);
model.Field(l => l.CustomerContactName);
model.Field(l => l.CustomerContactPhone);
model.Field(l => l.CustomerContactDuty);
}
)
.Read(read => read.Action("CustomersContactsRead", "Customers", new { customerID = "#=CustomerID#" }))
.Update(update => update.Action("CustomersContactsEdit", "Customers"))
.Create(update => update.Action("CustomersContactsCreate", "Customers", new { customerID = "#=CustomerID#" }))
)
.Events(e => e.Edit("onEdit"))
.Pageable()
.Sortable()
.Editable(editing => editing.Mode(GridEditMode.InCell))
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.ToClientTemplate())
);}))
Is there a braces code checker? Can you spot any error?
Thanx in advance!
The problem is this line
Content(obj => #(Html.Kendo().Grid<ModelApp.Models.CustomerContacts>()
In razor you should use template delegates:
Content(#<text>
#(Html.Kendo().Grid<ModelApp.Models.CustomerContacts>()