ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
Issue binding grid data during grouping with possible null node
Title:
B
I
{code}
?
My grid first loads great and all the data shows up correctly. However, *whenever I attempt to group by column* , I get the following error: Could not find property [TypeName] on type [Unit]. Make sure you specify Key and Column.Bind correctly. I get this same error message for TypeName, LocationName, and OwnerName. The issue is that *these properties just worked, when I displayed it on the page* . The only time it doesn't work is when I attempt to group. Here is my return Json Controller code: return Json(new GridModelBuilder<Unit>(list, g) { Key = "id", GetItem = () => db.Units.Find(Convert.ToInt32(g.Key)), Map = o => new { o.id, o.Type, TypeName = (o.UnitType != null) ? o.UnitType.Name : "", LocationName = (o.UnitLocation != null) ? o.UnitLocation.Name : "", OwnerName = (o.UserList != null) ? o.UserList.UserName : "", o.Location, o.Owner, InDate = (o.InDate != null) ? o.InDate.Value.ToString("MM/dd/yyyy h:mm tt") : "" } }.Build()); You will notice that TypeName, LocationName, and OwnerName all are using database navigation properties. How can I display o.UnitType.Name, o.UnitLocation.Name without getting this error? (I have tried to just put o.UnitType.Name without the if statement but it still throws an exception)
Save Changes
Cancel
eliawesome
asked at 01 Apr 2019
please read this: https://www.aspnetawesome.com/learn/mvc/Grid#Data-binding (and Map, Custom Formatting)
at 01 Apr 2019
Omu
Sorry for not seeing that area of documentation, but that fixed my issue. Thanks.
at 01 Apr 2019
eliawesome
I actually ran into a problem with only one column in my grid. My code: LeadUser = o.UserList.UserName, and new Column {Bind = "UserList.UserName", ClientFormat=".(LeadUser)", Header="Lead User"}, When I try and group by this column, I get the following error: (no errors on grid load) "Object reference not set to an instance of an object" - on the GetItems method. Any fixes for this or ideas why this may be happening?
at 01 Apr 2019
eliawesome
try the solution from here: https://www.aspnetawesome.com/forum/question/1333/awesome-grid-sort-on-column-bound-to-nested-property-with-possible-null-parent-ca basically instead of binding to `UserList.UserName` you need to create a new property that will have the null check in the get and bind to it
at 02 Apr 2019
Omu
That solution is exactly what I need, however, an issue I am having is that my class is not editable. `Unit` is generated by the database and is not manually editable. Right now if I change anything in `Unit` it is manually overridden by Entity Framework when I get the latest database definitions.
at 02 Apr 2019
eliawesome
maybe try inheriting the Generated type and add the property
at 02 Apr 2019
Omu
Using inheritance worked great. I was able to create a "SafeUserList" that did not contain any nulls. Now the Grid groups like it should and does not trigger errors. If anyone else is having this issue here is the solution: public partial class Unit { public string SafeUserList { get { return UserList != null ? UserList.UserName : ""; } } } The SafeUserList handles the navigation prop as well, and here is what my view code look like, new Column { Bind = "SafeUserList", ClientFormat=".(LeadUser)", Header="Lead User"} Thanks for all your help!!
at 02 Apr 2019
eliawesome
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