ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
Add a column header for grouping - not working with child entity
Title:
B
I
{code}
?
I am using a grid to show 2 SQL entities : Common Area Phones Sites there a relation 1 to n between (Sites and Common Area Phones). In my grid, I try to display Users data with a column retrieving Sites.site. It is diplayed correctly. But if I choose this column to group, I get an error in controller in the function GridGetItems. The error is "No property or field 'Site' exists in type 'CommonAreaPhones'" How to solve it ? The controller code is : private static object MapToGridModel(CommonAreaPhones o) { return new { Id = o.CommonAreaPhoneId, //this is needed to be able to edit/delete a row o.CommonAreaPhoneId, // Primary Key o.SIPAddressPhone, o.Sites.Site, // this is a navigation property o.SiteId, o.DisplayName, o.LineURI, o.TelephoneNumberDisplay, o.Description, o.VoicePolicy, o.PIN, o.CostCenter, o.SyncState, o.LastSyncDate, }; } public ActionResult GridGetItems(GridParams g, string search) { search = (search ?? "").ToLower(); IQueryable<CommonAreaPhones> items = null; // ARI View entity // Search for item that match the search" try { // Get the role of the user AccessUserProfile userProfile = (AccessUserProfile)Session["accessUserProfile"]; if (userProfile.userStatus == AccessUserProfile.UserStatus.FullAdmin) { // Full admin, so can manage all sites items = db.CommonAreaPhones.Where(o => o.DisplayName.ToLower().Contains(search)).AsQueryable(); } else { // Site admin, so can manage only his sites items = db.CommonAreaPhones.Where(o => userProfile.Sites.Contains(o.Sites.Site) && o.DisplayName.ToLower().Contains(search)).AsQueryable(); } return Json(new GridModelBuilder<CommonAreaPhones>(items, g) { Key = "CommonAreaPhoneId", // needed for api select, update, tree, nesting, EF // This method has been changed because a query is made against a View and not a table with a primary key //GetItem = () => db.CommonAreaPhones.Single(o => o.CommonAreaPhoneId == keyId), // called by the grid.api.update ( edit popupform success js func ) GetItem = () => db.CommonAreaPhones.Find(Convert.ToInt32(g.Key)), // called by the grid.api.update ( edit popupform success js func ) Map = MapToGridModel }.Build()); } catch (Exception ex) { .. } } The View code is : @Html.InitCrudPopupsForGrid("CommonAreaPhonesGrid", "CommonAreaPhones") @* popup names will be action + gridid; name is used when calling awe.open(name) *@ <div class="bar"> <button type="button" onclick="awe.open('createCommonAreaPhonesGrid')" class="awe-btn mbtn">Create</button> </div> @(Html.Awe().Grid("CommonAreaPhonesGrid") .Mod(o => o.PageInfo().ColumnsSelector().ColumnsAutohide()) .Url(Url.Action("GridGetItems", "CommonAreaPhones")) .Height(450) .Columns( new Column { Name = "CommonAreaPhoneId", Width = 65, Header = "Id" }, // ARI change Id to CostId new Column { Name = "SIPAddressPhone", Header = "SIP address" }.Mod(o => o.Nohide()), new Column { Name = "Site" }, new Column { Name = "DisplayName", Header = "Display Name" }, new Column { Name = "LineURI" }, new Column { Name = "TelephoneNumberDisplay", Header = "Number Display" }, new Column { Name = "Description"}, new Column { Name = "VoicePolicy", Header = "Voice Policy" }, new Column { Name = "PIN" }, new Column { Name = "CostCenter", Header = "Cost center" }, new Column { Name = "SyncState" }, new Column { Name = "LastSyncDate" }, new Column { ClientFormat = GridUtils.EditFormatForGrid("CommonAreaPhonesGrid", "Id"), Width = 50 }.Mod(o => o.Nohide()), new Column { ClientFormat = GridUtils.DeleteFormatForGrid("CommonAreaPhonesGrid", "Id"), Width = 50 }.Mod(o => o.Nohide())))
Save Changes
Cancel
Alain Rime
asked at 24 Feb 2016
Answers
B
I
{code}
?
it needs to be like this: new Column { Name = "Sites.Site", ClientFormat = ".Site" }, Name (Bind) needs to correspond to the Model (CommonAreaPhones), this is used by EF to generate SQL also you don't need .ToLower() because sql is case insensitive read more about this here: http://aspnetawesome.com/learn/mvc/Grid (Data binding and Map)
Save Changes
Cancel
Omu
answered at 25 Feb 2016
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