0

I have 2 classes such as Trail and National park as shown below having one to many relationship for ex one NationalPark can have many Trails

 public class Trail
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public double Distance { get; set; }
        public enum DifficultyType { Easy, Moderate, Difficult, Expert}
        public DifficultyType Difficulty { get; set; }

        [Required]
        public int NationalParkId { get; set; }
        [ForeignKey("NationalParkId")] 

        public NationalPark NationalPark { get; set; }
        public DateTime DateCreated { get; set; }
    }

 public class NationalPark

    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string State { get; set; }
        public DateTime Created { get; set; }
        public byte[] Picture { get; set; }
        public DateTime Established { get; set; }
    }

By using the below code I am getting every field in NationalPark class as shown below

 public Trail GetTrail(int trailId)     
{

            return _db.Trails.Include(c => c.NationalPark).FirstOrDefault(a => a.Id == trailId);

}

this is the result

[ 
{
    "id": 1,
    "name": "Trail1,Np3",
    "distance": 114,
    "difficulty": 2,
    "nationalParkId": 3,
    "nationalPark": {
      "id": 3,
      "name": "Test2",
      "state": "MP",
      "created": "2008-10-11T13:23:44",
      "picture": null,
      "established": "2002-10-11T13:23:44"
    }

]

I wanted to avoid the "picture" field in the above result, please help Thanks in Advance

  • check here. https://stackoverflow.com/questions/16455837/remove-null-values-in-json-and-update-json – Rajesh G Jun 09 '21 at 18:46

2 Answers2

0

You can create one Model, let's say NationalParkDetails which don't have a picture field and in its constructor set the required values.

public class NationalParkDetails
{
    public NationalParkDetails(NationalPark nationalPark){
      Id = nationalPark.Id;
      ..............other required fields.
    }
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string State { get; set; }
    public DateTime Created { get; set; }
    public DateTime Established { get; set; }
}

This model can then be used for the result.

  • It doesn't work for me, there is an error stating that Trail doesn't contain a definition for NationalParkDetail, anyway thanks. – santosh perne Jun 09 '21 at 19:34
0

Do you mean you want to ignore the null value when you convert to the json Result?

If this is your requirement, I suggest you could try to set the Ignore null property when serialize the Trail model to json result.

Since the Trail contains the picture by default. That means it will contains the null when convert to json file.

Below is asp.net 5's codes, if you use asp.net core 3.1, I suggest you could refer to this answer.

    services.AddControllersWithViews().AddJsonOptions(options => {
            options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
        }); ;

If you just want to ignore the picture propertied. The right way is creating a new model which is used to render the model to json result.

Like blow: public class NewTrail { [Key] public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public double Distance { get; set; }
    public enum DifficultyType { Easy, Moderate, Difficult, Expert}
    public DifficultyType Difficulty { get; set; }

    [Required]
    public int NationalParkId { get; set; }
    [ForeignKey("NationalParkId")] 

    public NewNationalPark NationalPark { get; set; }
    public DateTime DateCreated { get; set; }
}

public class NewNationalPark

{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string State { get; set; }
    public DateTime Created { get; set; }

    public DateTime Established { get; set; }
}

Then you should create a new model inside your action method and set the value to that NewTrail according to the selected Trail value.

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65