Hi Farveen,
The example you have sent me does not work please change it to read and write data to a MSSQL express database , that is what I am trying to do your example does not use EF and external sql database
Regards
Edmund Herbert
<ej-grid id="FlatGrid" allow-paging="true" >
<e-datamanager json="(IEnumerable<object>)ViewBag.datasource" insert-url="/Home/NormalInsert" adaptor="remoteSaveAdaptor" />
<e-edit-settings allow-adding="true"/>
<e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string> { "add", "update", "cancel"})"/>
<e-columns>
<e-column field="OrderID" header-text="Order ID" text-align="Right" is-primary-key="true"></e-column>
. ..
. ..
</e-columns>
</ej-grid>
public ActionResult NormalInsert([FromBody]CRUDModel<Orders> param)
{
_context.Add(param.Value);
_context.SaveChangesAsync();
return Json(param.Value);
}
public IActionResult Index()
{
ViewBag.datasource = (IEnumerable)_context.Orders.ToList();
return View();
} |
<ej-grid id="FlatGrid" allow-paging="true" >
<e-datamanager json="(IEnumerable<object>)ViewBag.datasource" insert-url="/Home/NormalInsert" adaptor="remoteSaveAdaptor" />
<e-edit-settings allow-adding="true"/>
<e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string> { "add", "update", "cancel"})"/>
<e-columns>
<e-column field="OrderID" header-text="Order ID" text-align="Right" is-primary-key="true"></e-column>
. ..
. ..
</e-columns>
</ej-grid>
public ActionResult NormalInsert([FromBody]CRUDModel<Orders> param)
{
_context.Add(param.Value);
_context.SaveChangesAsync();
return Json(param.Value);
}
public IActionResult Index()
{
ViewBag.datasource = (IEnumerable)_context.Orders.ToList();
return View();
} |
[Index.cshtml]
<ej-grid id="Grid" allow-paging="true" is-responsive="true" min-width="500" >
<e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" edit-mode="Normal"></e-edit-settings>
<e-toolbar-settings show-toolbar="true" toolbar-items='@new List<string> { "add", "edit","delete","update", "cancel" }'></e-toolbar-settings>
<e-datamanager adaptor="UrlAdaptor" insert-url="/Home/Insert" update-url="/Home/Update" remove-url="/Home/Delete" url="/Home/DataSource"></e-datamanager>
<e-columns>
<e-column field="OrderID" is-primary-key="true" visible="false" is-identity="true"></e-column>
<e-column field="EmployeeID" header-text="EmployeeID"></e-column>
<e-column field="ShipCity" header-text="Ship City"></e-column>
</e-columns>
</ej-grid>
[HomeController]
private NORTHWNDContext _context;
public HomeController(NORTHWNDContext context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
public ActionResult DataSource([FromBody]DataManager dm)
{
IEnumerable data = _context.Orders.ToList();
DataOperations operation = new DataOperations();
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
data = operation.PerformSorting(data, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
data = operation.PerformWhereFilter(data, dm.Where, dm.Where[0].Operator);
}
int count = data.Cast<Orders>().Count();
if (dm.Skip != 0)
{
data = operation.PerformSkip(data, dm.Skip);
}
if (dm.Take != 0)
{
data = operation.PerformTake(data, dm.Take);
}
return Json(new { result = data, count = count });
}
public ActionResult Insert([FromBody]CRUDModel<Orders> param)
{
_context.Orders.Add(param.Value); //record to be added in DB
_context.SaveChanges();
var data = _context.Orders.ToList();
return Json(param.Value); //return the added records
}
public ActionResult Update([FromBody]CRUDModel<Orders> param)
{
var db = _context;
db.Entry(param.Value).State = EntityState.Modified;
db.SaveChanges();
return Json(param.Value);
}
public ActionResult Delete([FromBody]CRUDModel<Orders> param)
{
var db = _context;
Orders order = db.Orders.Where(c => c.OrderID == Convert.ToInt32(param.Key)).FirstOrDefault();
db.Orders.Remove(order);
db.SaveChanges();
return Json(order);
}
|