ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
Awesome Grid sort on column bound to nested property with possible null parent cause errors
Title:
B
I
{code}
?
Hi, in my controllers, I have the following code: public ActionResult GridGetItems(GridParams g, string search) { search = search.ToEmptyStringIfNull().ToLower(); var items = _metService.GetAll() .Where( x => x.MetricCode.ContainsValue(search) || x.MetricName.ContainsValue(search) || x.IdUnitOfMeasureRefMetNavigation.ValueRef.ContainsValue(search) ) .OrderBy(x => x.MetricOrder) .ToList().AsQueryable(); var model = new GridModelBuilder<MetricMet>(items, g) { Key = "Id", // needed for api select, update, tree, nesting, EF GetItem = () => items.FirstOrDefault(x => x.Id == g.Key.ToGuid()), Map = MapEntityToGridModel }.Build(); return Json(model); } protected override object MapEntityToGridModel(MetricMet o) { var met = new { Id = o.IdMet, MetricOrderMet = o.MetricOrder, MetricCodeMet = o.MetricCode, MetricNameMet = o.MetricName, UnitOfMeasureMet = o.IdUnitOfMeasureRefMetNavigation?.ValueRef, ParentMet = o.IdParentMetNavigation?.MetricName, ExchangeRateTypeMet = o.IdExchangeRateTypeRefMetNavigation?.ValueRef, MetricTypeMet = o.IdMetricTypeRefMetNavigation?.ValueRef, IsActiveMet = o.IsActive, CreatedMet = o.CreatedMet.ToShortDateTimeString(), UpdatedMet = o.UpdatedMet.ToShortDateTimeString() }; return met; } View code: <div class="bar"> @(Html.Awe() .TextBox("txtsearch") .Placeholder("search ...") .CssClass("awe-searchtxt long-text-search")) </div> @(Html.Awe().Grid("MetGrid") .Mod(o => o.PageInfo().ColumnsSelector().ColumnsAutohide()) .Url(Url.Action("GridGetItems", "Metric")) .Resizable() .Groupable(false) .PageSize(30) .Reorderable() .Height(450) .Parent("txtsearch", "search", false) .Columns( new Column { Bind = "Id", Width = 55, Hidden = true }, new Column { Header = Localizer["MetricOrder"], Bind = "MetricOrder", ClientFormat = ".MetricOrderMet", Width = 110, CssClass = "align-right-grid-column", Sort = Sort.Asc }.Mod(o => o.Nohide()), new Column { Header = Localizer["MetricCode"], Bind = "MetricCode", ClientFormat = ".MetricCodeMet", Width = 110, CssClass = "align-center-grid-column" }.Mod(o => o.Nohide()), new Column { Header = Localizer["MetricName"], Bind = "MetricName", ClientFormat = ".MetricNameMet" }.Mod(o => o.Nohide()), new Column { Header = Localizer["UnitOfMeasure"], Bind = "IdUnitOfMeasureRefMetNavigation.ValueRef", ClientFormat = ".UnitOfMeasureMet", Width = 140 }.Mod(o => o.Nohide()), new Column { Header = Localizer["ParentMetric"], Bind = "IdParentMetNavigation.MetricName", ClientFormat = ".ParentMet", Sortable = false, Width = 250 }.Mod(o => o.Nohide()), new Column { Header = Localizer["ExchangeRateType"], Bind = "IdExchangeRateTypeRefMetNavigation.ValueRef", ClientFormat = ".ExchangeRateTypeMet", Sortable = true, Width = 160 }.Mod(o => o.Nohide()), new Column { Header = Localizer["MetricType"], Bind = "IdMetricTypeRefMetNavigation.ValueRef", ClientFormat = ".MetricTypeMet", Sortable = false, Width = 120}.Mod(o => o.Nohide()), new Column { Header = Localizer["IsActive"], Bind = "IsActive", ClientFormat = ".IsActiveMet", ClientFormatFunc = "toggleButton", Width = 100}.Mod(o => o.Nohide()), //new Column { Header = Localizer["Created"], Bind = "CreatedMet", ClientFormat = ".CreatedMet", Width = 150 }.Mod(o => o.Autohide(2)), //new Column { Header = Localizer["Updated"], Bind = "UpdatedMet", ClientFormat = ".UpdatedMet", Width = 150 }.Mod(o => o.Autohide(1)), new Column { ClientFormat = GridUtils.EditFormatForGrid("MetGrid"), Width = 50 }.Mod(o => o.Nohide()), new Column { ClientFormat = GridUtils.DeleteFormatForGrid("MetGrid"), Width = 50 }.Mod(o => o.Nohide()))) When I click on this 2 columns, the error happens: ".ParentMet" ".MetricTypeMet" some on my grid columns are nullable values ex: parent item. this causes errors happens in this code in the sort awe stuff. I totally get why but I can't figure a way to trap or fix it. " at lambda_method(Closure , MetricMet )\r\n at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)\r\n at System.Linq.EnumerableSorter`1.ComputeMap(TElement[] elements, Int32 count)\r\n at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count, Int32 minIdx, Int32 maxIdx)\r\n at System.Linq.OrderedEnumerable`1. <GetEnumerator>d__7.MoveNext()\r\n at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)\r\n at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)\r\n at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)\r\n at Omu.AwesomeMvc.GridModelBuilder`1.BuildModel()\r\n at Omu.AwesomeMvc.GridModelBuilder`1.Build()\r\n at ..." Do you a way to workaround this effect ? FYI, my ID primary keys are of type GUID Thanks -Vince
Save Changes
Cancel
vRITHNER
asked at 08 Sep 2017
try adding in `MetricMet` class a property like this: public string IdUnitOfMeasureRefMetNavigationValueRef { get { return IdUnitOfMeasureRefMetNavigation == null ? null : IdUnitOfMeasureRefMetNavigation.ValueRef; } } and in the view set the `Column.Bind` to `IdUnitOfMeasureRefMetNavigationValueRef`
at 11 Sep 2017
Omu
Hello, sorry for the delay. emergency to deliver... Anyway, I just tried your suggestion and it is working fine. my wish is for you to support this implicitly in a next version ;-) Txs very much -v
at 14 Sep 2017
vRITHNER
Answers
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