I just starting out with MVC so sorry if I'm missing something obvious:
I want to use Entity Framework with ViewModels but I'm having a issue upon updating.
This issue I'm having is that the entity contains many more properties
then are displayed in the grid, but when it goes to the Update Action
the 'value' (entity) parameter only contains an entity where its values are only the ones displayed in the grid
and nulls all the other values. It seems like it only sends the Update
Action a new entity with only values in the grid as opposed to the
original entity with updated properties. Is this by design? I'd much
rather have the original entity that contains changed values.
The view looks like this:@model IEnumerable<SyncfusionMvcApplication1.Request>
@using SyncfusionMvcApplication1
@{
}
<h2>GridFeatures</h2>
<div id="ControlRegion">
@(Html.EJ().Grid<Request>("FlatGrid")
.Datasource(ds => ds.Json(Model).UpdateURL("/Requests/Update").InsertURL("/Requests/Insert").RemoveURL("/Requests/Remove").Adaptor(AdaptorType.RemoteSaveAdaptor))
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
.EnablePersistence(true)
.ToolbarSettings(toolbar =>
{
toolbar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.Add);
items.AddTool(ToolBarItems.Edit);
items.AddTool(ToolBarItems.Delete);
items.AddTool(ToolBarItems.Update);
items.AddTool(ToolBarItems.Cancel);
});
})
.Columns(col =>
{
col.Field(p=>p.RequestID).HeaderText("Request ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
col.Field(p=>p.BU).HeaderText("BU").Width(80).Add();
col.Field(p => p.NewExpand).HeaderText("New / Expand / Refresh").Width(80).EditType(EditingType.Dropdown).DataSource((List<KeyPair>)ViewBag.ddNewExp).Add();
}))
The controller looks like this: public class RequestsController : Controller
{
private BudgetEntities db = new BudgetEntities();
public ActionResult Grid()
{
return View(db.Requests.ToList());
}
public ActionResult Update(Request value) /
/the value param only contains a entity with values that only appear in grid (the rest are null), NOT the original entity with updated values. {
Request req = db.Requests.Single(o => o.RequestID == value.RequestID);
db.Entry(req).CurrentValues.SetValues(value);
db.Entry(req).State = EntityState.Modified;
req.BU = value.BU;
try
{
db.SaveChanges();
}
catch (Exception ex)
{
throw;
}
var result = db.Requests.ToList();
return Json(new { result = result, count = result.Count }, JsonRequestBehavior.AllowGet);
}