ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
Mapping in Grid, Crud Popup not closed
Title:
B
I
{code}
?
I dont quite understand the line below. Can you please explain that? If I want to pass a viewmodel combining two tables. How can I pass the viewmodel to this line of code?: GetItem = () => _context.EngUnits.FirstOrDefault(x => x.Id == Id)
Save Changes
Cancel
minh yrrrt
asked at 23 Feb 2015
please see my updated answer
at 24 Feb 2015
Omu
did the answer fix your problem ?
at 25 Feb 2015
Omu
Thanks a lot for your answer. My problem was much simpler than that. My intpopup did not close because I did not pass id into it. I found the bug. But where can I find infor about using api here?
at 26 Feb 2015
minh yrrrt
I guess you needed awe.open('editPopupName', { params:{ id: 23 } }) most info you get from the demo app, there's also some in the docs
at 27 Feb 2015
Omu
That's exactly right, Thanks a lot!
at 28 Feb 2015
minh yrrrt
Answers
B
I
{code}
?
GetItem is used by the grid.api.update, which in the demo is used for the EditPopupForm Success js function, you don't need to pass viewmodel to it because the map is going to be applied to its result inside the GridModelBuilder before being passed to the grid (client) if you're doing grid crud by following the demo project have a look at this newer demo which also is using different viewmodels for grid and create/edit popups: please note in the code below the MapToGridModel is used in the GridGetItems and Create actions also you could have a problem because the edit js func atm assumes you're key is "Id", instead of using the grid key public class DinnersGridCrudController : Controller { private static object MapToGridModel(Dinner o) { return new { o.Id, o.Name, o.Date, ChefName = o.Chef.FirstName + " " + o.Chef.LastName, Meals = string.Join(",", o.Meals.Select(m => m.Name)) }; } public ActionResult GridGetItems(GridParams g) { return Json(new GridModelBuilder<Dinner>(Db.Dinners.AsQueryable(), g) { Key = "Id", // needed for api select, update, tree, nesting, EF GetItem = () => Db.Get<Dinner>(Convert.ToInt32(g.Key)), // called by the grid.api.update ( edit popupform success js func ) Map = MapToGridModel }.Build()); } public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(DinnerInput input) { if (!ModelState.IsValid) return View(input); var dinner = Db.Insert(new Dinner { Name = input.Name, Date = input.Date.Value, Chef = Db.Get<Chef>(input.Chef), Meals = Db.Meals.Where(o => input.Meals.Contains(o.Id)) }); return Json(MapToGridModel(dinner)); // returning grid model, used in grid.api.renderRow } public ActionResult Edit(int id) { var dinner = Db.Get<Dinner>(id); var input = new DinnerInput { Id = dinner.Id, Name = dinner.Name, Chef = dinner.Chef.Id, Date = dinner.Date, Meals = dinner.Meals.Select(o => o.Id) }; return View("create", input); } [HttpPost] public ActionResult Edit(DinnerInput input) { if (!ModelState.IsValid) return View("create", input); var dinner = Db.Get<Dinner>(input.Id); dinner.Name = input.Name; dinner.Date = input.Date.Value; dinner.Chef = Db.Get<Chef>(input.Chef); dinner.Meals = Db.Meals.Where(m => input.Meals.Contains(m.Id)); Db.Update(dinner); // returning the key to call grid.api.update return Json(new { dinner.Id }); } ... also here's updated js functions that don't assume that the key is "Id" but use the grid key instead: function itemDeleted(gridId) { return function (res) { var $grid = $("#" + gridId); var key = $grid.data('o').ck || $grid.data('o').lsrs.Key; $grid.data('api').select(res[key])[0].fadeOut(500, function () { $(this).remove(); if (!$grid.find('.awe-row').length) $grid.data('api').load(); }); }; } function itemUpdated(gridId) { return function (item) { var $grid = $('#' + gridId); var api = $grid.data('api'); var key = $grid.data('o').ck || $grid.data('o').lsrs.Key; var xhr = api.update(item[key]); $.when(xhr).done(function () { var $row = api.select(item[key])[0]; var altcl = $row.hasClass("awe-alt") ? "awe-alt" : ""; $row.switchClass(altcl, "awe-changing", 1).switchClass("awe-changing", altcl, 1000); }); }; }
Save Changes
Cancel
Omu
answered at 23 Feb 2015
please
Sign In
to leave an answer
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our
cookie policy
and
privacy policy
.
OK