Hi
I'm trying to get a custom dialog template working with a dropdown and can't get the drop down to populate. If I don't use a custom template the dropdown populates as expected. But I can't figure out how to get the dropdown list to populate in the custom template.
This is the main view.
@{
ViewData["Title"] = "Custom Messages";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Use the form below to add, edit or delete custom messages for your Connect contact centre solution.</p>
</div>
@{
var ActionDropDownList = new Syncfusion.EJ2.DropDowns.DropDownList()
{
DataSource = ViewBag.ActionDropDownData,
Query = "new ej.data.Query()",
AllowFiltering = false
};
}
<div>
<ejs-grid id="Grid" allowPaging="true" toolbar="@(new List<string>() { "Add", "Edit", "Delete"})" actionComplete="actionComplete">
<e-data-manager url="/Home/UrlDataSource" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Delete" adaptor="UrlAdaptor" ></e-data-manager>
@if (@ViewData["admin"].ToString() == "true")
{
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog" template='#dialogtemplate'></e-grid-editSettings>
}
<e-grid-columns>
<e-grid-column field="name" headerText="Name" textAlign="Left" width="120" validationRules="@(new { required=true})"></e-grid-column>
<e-grid-column field="message" headerText="Message" width="150"></e-grid-column>
<e-grid-column field="enabled" type="boolean" headerText="Enabled" textAlign="Left" editType="booleanedit" displayAsCheckBox="true" width="130"></e-grid-column>
<e-grid-column field="dateStart" headerText="Start Date" width="140" format="dd/MM/yyyy HH:mm" editType="datetimepickeredit" validationRules="@(new { required=true})"></e-grid-column>
<e-grid-column field="dateEnd" headerText="End Date" width="150" format="dd/MM/yyyy HH:mm" editType="datetimepickeredit" validationRules="@(new { required=true})"></e-grid-column>
<e-grid-column field="action" headerText="Action" width="150" editType="dropdownedit" edit="new {@params = ActionDropDownList }"></e-grid-column>
</e-grid-columns>
</ejs-grid>
</div>
then the partial view for the add includes...
<ejs-dropdownlist id="Action" dataSource="@ViewBag.ActionDropDownData" placeholder="Action" floatLabelType="Always" popupHeight="300px">
</ejs-dropdownlist>
and the controller defines ViewBag.ActionDropDownData as an array of strings. This is because I need to alter the list of possible values in the drop down depending on who is logged into the site.
How do I get the custom template to populate like the normal dialog does in this situation?
|
Index.cshtml
ejs-grid id="Grid" actionComplete="actionComplete" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })" allowPaging="true">
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog" template='#dialogtemplate'></e-grid-editSettings>
<e-data-manager url="/Home/UrlDataSource" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Delete" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" validationRules="@(new { required=true, number=true})" width="140"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer Name" validationRules="@(new { required=true})" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" validationRules="@(new { required=true})" textAlign="Right" editType="numericedit" format="C2" width="140"></e-grid-column>
<e-grid-column field="ShipCountry" headerText="Ship Country" width="150" editType='dropdownedit'></e-grid-column>
<e-grid-column field="ShipCity" headerText="Ship city" width="150" editType='dropdownedit'></e-grid-column>
</e-grid-columns>
</ejs-grid>
<script id='dialogtemplate' type="text/x-template">
<div id="dialogTemp">
</div>
</script>
<script>
function actionComplete(args) {
if (args.requestType === 'beginEdit' || args.requestType === 'add') {
let spinner = ej.popups.createSpinner({ target: args.dialog.element });
ej.popups.showSpinner(args.dialog.element);
if (args.requestType === 'beginEdit') {
var ajax = new ej.base.Ajax({
url: "@Url.Action("EditPartial")", //render the partial view
type: "POST",
contentType: "application/json",
data: JSON.stringify({ value: args.rowData })
});
ajax.send().then(function (data) {
appendElement(data, args.form); //Render the edit form with selected record
args.form.elements.namedItem('CustomerID').focus();
ej.popups.hideSpinner(args.dialog.element);
}).catch(function (xhr) {
console.log(xhr);s
ej.popups.hideSpinner(args.dialog.element);
});
}
if (args.requestType === 'add') {
var ajax = new ej.base.Ajax({
url: "@Url.Action("AddPartial")", //render the partial view
type: "POST",
contentType: "application/json",
data: JSON.stringify({ value: args.rowData })
});
ajax.send().then(function (data) {
appendElement(data, args.form); //Render the edit form with selected record
args.form.elements.namedItem('OrderID').focus();
ej.popups.hideSpinner(args.dialog.element);
}).catch(function (xhr) {
console.log(xhr);
ej.popups.hideSpinner(args.dialog.element);
});
}
}
}
function appendElement(elementString, form) {
form.querySelector("#dialogTemp").innerHTML = elementString;
var script = document.createElement('script');
script.type = "text/javascript";
var serverScript = form.querySelector("#dialogTemp").querySelector('script');
script.textContent = serverScript.innerHTML;
document.head.appendChild(script);
serverScript.remove();
}
</script>
|
|
HomeController.cs
public IActionResult EditPartial([FromBody] CRUDModel<OrdersDetails> value) {
// datasourse for custom dropdown component. ViewBag.county= CountryData.GetDropData().ToList();
ViewBag.city = DDCityData.GetDropData().ToList();
return PartialView("_DialogEditPartial", value.Value);
}
public IActionResult AddPartial([FromBody] CRUDModel<OrdersDetails> value)
{
ViewBag.county = CountryData.GetDropData().ToList();
ViewBag.city = DDCityData.GetDropData().ToList();
return PartialView("_DialogAddPartial");
} |
|
DialogAddPartial.cshtml
@model EJ2Grid.Models.OrdersDetails
@*//define the model for store the model values*@
@using Syncfusion.EJ2
<div>
<div class="form-row">
<div class="form-group col-md-6">
<div class="e-float-input e-control-wrapper">
<input asp-for="OrderID" type='text' />
<span class="e-float-line"></span>
<label asp-for="OrderID" class="e-float-text e-label-top">Order ID</label>
</div>
</div>
<div class="form-group col-md-6">
<div class="e-float-input e-control-wrapper">
<input asp-for="CustomerID" />
<span class="e-float-line"></span>
<label asp-for="CustomerID" class="e-float-text e-label-top">Customer Name</label>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<ejs-numerictextbox id="Freight" format="C2" placeholder="Freight" floatLabelType="Always"></ejs-numerictextbox>
</div>
<div class="form-group col-md-6">
<ejs-dropdownlist id="ShipCountry" dataSource="@ViewBag.county" placeholder="Ship Country" floatLabelType="Always" popupHeight="300px">
<e-dropdownlist-fields text="CountryName" value="CountryName"></e-dropdownlist-fields>
</ejs-dropdownlist>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<ejs-dropdownlist id="ShipCity" dataSource="@ViewBag.city" placeholder="Ship City" floatLabelType="Always" popupHeight="300px">
<e-dropdownlist-fields text="CityName" value="CityName"></e-dropdownlist-fields>
</ejs-dropdownlist>
</div>
</div>
</div>
<ejs-scripts></ejs-scripts> |
|
|
Perfect, that example code has solved my problem, I can see where I went wrong and have fixed it, wasn't including the ViewBag data in the EditPartial and AddPartial methods.
Thank you for your help