I am using a URLAdaptor on my grid with individual URL's for Insert, Update and Remove.
I want to get the data passed from the client mapped to my model.
I have tried using the same syntax as used in your examples [FromBody]CRUDModel<model> variable, for instance in another forum thread where you supplied following example:
My controller method:
public IActionResult InsertUrl([FromBody]CRUDModel<MdxMemberAttribute> crudmodel)
{
MdxMemberAttribute abc = crudmodel.Value;
abc.Id = Guid.NewGuid(); // To be replaced by Guid returned from Back End API
_dbContext.Add(abc);
_dbContext.SaveChanges();
crudmodel.Value.Id = abc.Id;
return Json(crudmodel.Value);
}
However in this case crudmodel is Null and thus I cannot assign it to abc
I have tried variations like the following:
1. public IActionResult InsertUrl([FromBody]CRUDModel crudmodel)
2. public IActionResult InsertUrl([FromBody]MdxMemberAttribute mdxMemberAttribute)
Only in the first case I get the data from the grid but then I cannot cast it to my model (error: Cannot implicitly convert type 'object' to 'MdxMemberAttribute').
Question 1. Can you let me know how I can cast the data coming from the grid to my model?
Question 2: Before saving the data to the database I need to retrieve the value for the primary key. The grid needs the value for the PK as well. If I use crudmodel.Value.Id = abc.Id and then return crudmodel.Value, will this update the grid correctly? If not, how should this be done?
For completeness I've added my grid definition below:
<div>
<ejs-grid id="attributeGrid" toolbar="@(new List<string>() { "Add","Edit", "Delete", "Update", "Cancel", "ExcelExport" })" toolbarClick="toolbarClick" allowExcelExport="true" allowPaging="true" allowSelection="true" allowSorting="true" allowFiltering="true" allowMultiSorting="false" query="new ej.data.Query().addParams('CatalogGuid', '@ViewData["catalogGuid"]').addParams('EntityGuid', '@ViewData["entityGuid"]')">
<e-data-manager url="/LibMan/AttributeDatasource" adaptor="UrlAdaptor" insertUrl="/LibMan/InsertUrl" updateUrl="/LibMan/UpdateUrl" removeUrl="/LibMan/DeleteUrl" BatchUrl="/LibMan/BatchUrl"></e-data-manager>
<e-grid-editSettings allowEditing="true" allowAdding="true" allowDeleting="true" mode="Normal" showConfirmDialog="true" allowEditOnDblClick="true"></e-grid-editSettings>
<e-grid-pagesettings pageCount="5" pageSize="50" pageSizes="@(new string[] { "15", "20", "25", "50", "All" })"></e-grid-pagesettings>
<e-grid-filterSettings type="Menu" mode="OnEnter" immediateModeDelay="55"></e-grid-filterSettings>
<e-grid-columns>
<e-grid-column field="Id" isPrimaryKey="true" visible="false" showInColumnChooser="false"></e-grid-column>
<e-grid-column field="MdxCatalogGuid" defaultValue="@ViewData["catalogGuid"]" visible="false" showInColumnChooser="false"></e-grid-column>
<e-grid-column field="MdxEntityGuid" defaultValue="@ViewData["entityGuid"]" visible="false" showInColumnChooser="false"></e-grid-column>
<e-grid-column field="Name"></e-grid-column>
<e-grid-column field="MdxAttributeType"></e-grid-column>
<e-grid-column field="IsMandatory" displayAsCheckBox="true" editType="checkboxedit"></e-grid-column>
<e-grid-column field="RequiresValidation" displayAsCheckBox="true"></e-grid-column>
<e-grid-column field="TextLength" format="N0"></e-grid-column>
<e-grid-column field="NumberDecimals" format="N0"></e-grid-column>
<e-grid-column field="DateTimeFormat"></e-grid-column>
<e-grid-column field="LookupEntity"></e-grid-column>
<e-grid-column field="LookupFilter"></e-grid-column>
<e-grid-column field="ChangeTrackingEnabled" displayAsCheckBox="true"></e-grid-column>
<e-grid-column field="ChangeTrackingGroup" format="N0"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<ejs-scripts></ejs-scripts>
</div>
thanks
|
public ActionResult Insert([FromBody]CRUDModel<OrdersDetails> value)
{
OrdersDetails.GetAllRecords().Insert(0, value.value);
OrdersDetails mobj = value.value;
mobj.OrderID = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0);
value.value.OrderID = mobj.OrderID;
return Json(value.value);
}
...
public class OrdersDetails
{
public static List<OrdersDetails> order = new List<OrdersDetails>();
public OrdersDetails()
{
}
public OrdersDetails(int OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress)
{
...
}
public static List<OrdersDetails> GetAllRecords()
{
...
}
public int? OrderID { get; set; }
...
} |
|
public ActionResult Insert([FromBody]CRUDModel<OrdersDetails> value)
{
OrdersDetails.GetAllRecords().Insert(0, value.value);
OrdersDetails mobj = value.value;
mobj.OrderID = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0);
value.value.OrderID = mobj.OrderID;
return Json(value.value);
} |
|
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().
AddJsonOptions(options =>
{
// JSON serialization not defaulting to default?
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
} |
|
public ActionResult Insert([FromBody]CRUDModel<Employee1Details> value)
{
Employee1Details.GetAllRecords().Insert(0, value.value);
return Json(value.value);
}
public class CRUDModel<T> where T : class // Generic class
{
public string action { get; set; }
public string table { get; set; }
public T value { get; set; }
. . . . .
} |
|
|