Zayco Zayco - 1 year ago 67
C# Question

Accessing related data in if statment inside foreach

I have 3 tables. Vlans, IPNets and Equipments

Vlans has a one to many relationship with IPNets, and Equipmentshas a one to many relationship with IPNets.

in my details view for Vlans I have this foreach.

@foreach (var item in Model.IPNets)
@Html.DisplayFor(modelItem => item.Equipment.Description)
@Html.DisplayFor(modelItem => item.IPAddresse)
@Html.DisplayFor(modelItem => item.Equipment.Name)
@Html.DisplayFor(modelItem => item.Equipment.EquipmentTypes.Name)
@Html.DisplayFor(modelItem => item.Equipment.Departments.Name)
@Html.DisplayFor(modelItem => item.Equipment.Location)
@if (item.Equipment.PacsId == 1)
<small class="label bg-green">PACS</small>

The modelitems are working fine and generates a table with alle items in IPNets matching the relationship with Vlans.

The problem is the if statement. As it is now I get "object is not set to an instance of an object".

using item.EquipmentId which is the foreign key in IPNets is working but is not the property I want to use.

Using @if (modelItem => item.Equipment.PacsId == 1) gives error: Cannot convert lambda expression to type 'bool' because it is not a delegate type


public class VlansViewModel
public int VlanId { get; set; }

public string Zone { get; set; }

public string Number { get; set; }

public string VlanName { get; set; }

public string Info { get; set; }

public string IPNet { get; set; }

public string IPStart { get; set; }

public string IPEnd { get; set; }

public string Mask { get; set; }

public string Gateway { get; set; }

public Location? Location { get; set; }

public string Cidr { get; set; }

public virtual ICollection<IPNet> IPNets { get; set; }

public virtual IEnumerable<Equipment> Equipments { get; set; }

public VlansViewModel(Vlan vlan)
VlanId = vlan.VlanId;
Zone = vlan.Zone;
Number = vlan.Number;
VlanName = vlan.Name;
Info = vlan.Info;
IPNet = vlan.IPNet;
IPStart = vlan.IPStart;
IPEnd = vlan.IPEnd;
Mask = vlan.Mask;
Gateway = vlan.Gateway;
Cidr = vlan.Cidr;
IPNets = vlan.IPNets;
Location = vlan.Location;



// GET: Vlans/Details/5
public ActionResult Details(int? id)
// Get current Id
var vlansid = db.Vlans.Find(id);

var ViewModel = new VlansViewModel(vlansid);

return View("Details", ViewModel);

Any suggestions how i can use item.Equipment.PacsId in my if statement?


As Shyju pointed out, the Equipment property is indeed null when the if statement is included. But I just dont understand how to include the property when Vlans don't have a relation with it.

I guess I would need to have a query in the controller which includes Equipments where EquipmentId equals IPNets.EquipmentId, but I have been trying to figure this out for so long now that everything is starting to merge into one big blur.

And I also don't understand why modelItem => item.Equipment.PacsId is loading the right values but item.Equipment.PacsId is giving null. I'm new to this and would appreciate if anyone could give an short explanation.

Answer Source

Check for null, and continue if not null:

@if (item.Equipment != null && item.Equipment.PacsId == 1)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download