Last post Apr 17, 2020 03:11 AM by bruce (sqlwork.com)
Apr 16, 2020 07:55 PM|bank5|LINK
I found this thread which I thought was pretty neat - https://forums.asp.net/t/1716207.aspx?ViewData+ModelMetadata+Properties+to+display+all+fields+in+a+model+exclude+null+values
I wasn't sure about the solution though as
Will always return true. What is suppose to be checked in the if statement to get the value of the property?
(I tried responding directly to the other thread but it's locked.)
EDIT: I wound up using this which is working as intended:
if (Model.GetType().GetProperty(prop.PropertyName).GetValue(Model).ToString() != "")
Apr 17, 2020 01:22 AM|Rena Ni|LINK
If you resolve your issue,please post your answer as a new post and mark it as answer.
Apr 17, 2020 03:11 AM|bruce (sqlwork.com)|LINK
the sample is looping thru the meta property data, not the type. it assumed if there is no PropertyName, then its not a matching model model property for binding. the idea of meta properties, is if your model is code generated (old ef models) you should
not edit it to add attributes to the properties. so MVC allows a meta attribute and partial classes to add attributes.
generally we use POCO objects now and add attributes as needed.