Hi,
I have a datatable which I use to bind to the EJ2 grid. The datarows of this datatable do not represent models, they simply contain data from other models in the project.
I can get the data to display, but I am having trouble finding a way of being able to edit the content of the grid and having those changes be passed to my controller so that I can update values in the database.
Any help would be much appreciated!
Thanks,
Craig
Hi again,
I have managed to cast my DataTable as IEnumerable<ExpanoObject>
There is definitely data in the
IEnumerable
<ExpanoObject>
View:
@Html.EJS().Grid("FTIRDriftCorrected").DataSource(ds => ds.Url(Url.Action("GridDataSource", "FTIRDriftCorrected")).Adaptor("UrlAdaptor")).Columns(col =>
{
foreach (DataColumn column in Model.DriftCorrectedValues.Columns)
{
if (column.ColumnName == "Deleted")
col.Field(column.ColumnName).HeaderText("").DisplayAsCheckBox(true).EditType("booleanedit").TextAlign(TextAlign.Center).Width("10%").Add();
else if (column.ColumnName == "Time")
col.HeaderText(column.ColumnName).Field(column.ColumnName).AllowEditing(false).TextAlign(TextAlign.Center).HeaderTextAlign(TextAlign.Center).Format("HH:mm").Add();
else
col.HeaderText(column.ColumnName).Field(column.ColumnName).TextAlign(TextAlign.Center).HeaderTextAlign(TextAlign.Center).Format("N1").Add();
}
}).EditSettings(e => { e.AllowAdding(false).AllowEditing(true).AllowDeleting(true).Mode(EditMode.Normal); }).Toolbar(new List
() { "Edit", "Update", "Cancel" }).AllowTextWrap(true).TextWrapSettings(text => { text.WrapMode(WrapMode.Header); }).Render()
Controller:
public ActionResult GridDataSource(DataManagerRequest dm)
{
IEnumerable
return Json(dataTableToObject, JsonRequestBehavior.AllowGet);
}
Extension Method:
public static IEnumerable
{
var dynamicDt = new List
foreach (DataRow row in dt.Rows)
{
dynamic dyn = new ExpandoObject();
dynamicDt.Add(dyn);
foreach (DataColumn col in dt.Columns)
{
var dic = (IDictionary
dic[col.ColumnName] = row[col];
}
}
return dynamicDt;
}
Thanks!
Thanks for your response.
I have managed to bind to the Grid without the need of a DataTable. I convert my data into List<ExpandoObject> and the grid is now binding properly (see below):
However, I am now struggling to access values on the server when trying to edit the grid.
Below you can see that there is data when I am passing to the View:
And below here you can see that there is no data in ExpandoObject when updating the Grid:
I hope I'm just missing something obvious.
Thanks again for your help.
|
//update the record
public ActionResult Update([FromBody]CRUDModel<ExpandoObject> value)
{
var ord = value.Value;
OrdersDetails val = OrdersDetails.GetAllRecords().Where(or => or.OrderID.Equals( ord.OrderID.ToString())).FirstOrDefault();
val.OrderID = ord.OrderID;
return Json(value.Value);
}
|
Hi again,
I now have the grid working properly. The issue was that the parameter name needed to be "value". I can access my values through casting the ExpandoObject as Dictionary<TKey, TValue>
However I would like to add one more thing in case others come across a similar issue.
I could not access my data in the CRUDModel<ExpandoObject> using you previous example, see below:
My issue is now resolved. Thank you for your help, it is much appreciated.