2

I have this class and many properties in it

    public class Course
    {
      public int CourseId { get; set; }
      public string CourseName { get; set; }
      public Universities? UniversityName { get; set;}
      public string Summary { get; set; }
      *
      *
    }

View Model looks like this

    public class DetailsViewModels
    {
      public Course DetailsVMCourse;
      public string PageTitle;
      *
    }

And controller

    public IActionResult CourseDetails(int id)
    {
        DetailsViewModels detailsViewModels = new DetailsViewModels()
        {
            //loads the course to the view model from a test database
            DetailsVMCourse = _courseRepository.FindCourse(id)
        };
        return View(detailsViewModels);
    }

I want to show only if a model property has value in the view. At the moment I am doing this on the view page for each property..

@{ if (Model.DetailsVMCourse.Summary != null)
  {
   <h4>Cource summary</h4>
   <p>
   @Model.DetailsVMCourse.Summary
   </p>
  }
}

Rather than checking every single property with !=null in view, what would be better and efficient way. This is my first project so your help would be greatly appriciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
aries
  • 45
  • 6
  • You would check it in the view as you are doing now. Or you can check it before you return from the action and can select a different view based on the model's result. – Jasen Jan 28 '20 at 23:40

2 Answers2

0

You could write something that would loop over every property in the class using reflection, but I'm thinking you really don't want that. The properties are of different types and I am sure there are some you may not actually want to show (IDs?). With some being other classes, it gets complicated (UniversityName is an object, not a string). Also, within your H4 Tag, the "title" is not the same as the property, it's more human-readable. So I think your best bet is to follow your current pattern and have a block for each property that you really want to show, along with its human-readable title.

My only other suggestion would be to use IsNullOrWhiteSpace() instead of the null check for the string properties. It's a cleaner/thorough check for strings:

if (!String.IsNullOrWhiteSpace(Model.DetailsVMCourse.Summary))
Bryan Lewis
  • 5,629
  • 4
  • 39
  • 45
  • You are correct, dont want to use reflection, as my properties are different types. Thank you for your help. :) – aries Jan 29 '20 at 00:24
  • @aries, no problem. If you feel this is an adequate answer, please mark this as the answer to close this question. – Bryan Lewis Jan 29 '20 at 00:49
0

I would use reflection to get the properties that have a value that is not equal to null, and then use a loop to display the properties and its values.

var populatedProperties = Model.DetailsVMCourse.GetType().GetProperties().Where(prop => prop.GetValue(Model.DetailsVMCourse) != null);

foreach (var prop in populatedProperties){
   <h4>@prop.Name</h4>
   <p>
   @prop.GetValue(Model.DetailsVMCourse);
   </p>
}
Jossean Yamil
  • 850
  • 2
  • 9
  • 22
  • Although properties are different types, but I can use this in a different class. Thanks for the idea. – aries Jan 29 '20 at 00:28