1

I'm trying to create a little asset management application, like an inventory. There are asset types (like notebook, mobile phone, desk, dishwasher etc.) and assets (like notebook-001, mobile-010 etc.).

So this is my AssetType model:

public class AssetType
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<AssetTypeMeta> AssetTypeMetas { get; set; }
}

And this is my Asset model:

public class Asset
{
    public int Id { get; set; }

    [ForeignKey("TypeId")]
    public AssetType AssetType { get; set; }
    public int TypeId { get; set; }

    public string SerialNumber { get; set; }
    public double PurchasePrice { get; set; }

    public ICollection<AssetMeta> FieldNames { get; set; }
    public ICollection<AssetTypeMeta> FieldValues { get; set; }
}

As you can see, there are only 2 fields in my asset table, SerialNumber and PurchasePrice. But I want user to be able to define new fields specific to the device type and be able to enter information to those fields. When I test it in my database, everything looks fine.

For this I have created two more models. One of them is AssetTypeMeta, which is meant to hold new (user-defined) field names for the user.

public class AssetTypeMeta
{
    public int Id { get; set; }
    [ForeignKey("AssetTypeId")]
    public AssetType AssetType { get; set; }
    public int AssetTypeId { get; set; }
    public string FieldName { get; set; }
}

And I've added a new model, AssetMeta, which is a link between an asset type, an asset and user defined fields for that asset type.

public class AssetMeta
{
    public int Id { get; set; }
    [ForeignKey("AssetTypeMetaId")]
    public AssetTypeMeta AssetTypeMeta { get; set; }
    public int AssetTypeMetaId { get; set; }

    [ForeignKey("AssetId")]
    public Asset Asset { get; set; }
    public int AssetId { get; set; }
    public string? FieldValue{ get; set; }
}

I can define fields like IMEI, NFC, Operator etc. for the device type "Mobile Phone" in AssetTypeMeta table and link them to the actual devices in AssetMeta table, like mobile-phone01's IMEI is 11111111111 and Operator is Vodafone etc.

But I can't make all those information visible in my page. For example, if a user clicks on the device mobile-phone01, I have to also show the user defined fields for that device, like IMEI and Operator (even if they don't have any values assigned in AssetMeta table).

How do I set up my viewmodel, action and view to do that?

oknsyl
  • 57
  • 1
  • 9
  • You can add text column in the table (userDefinedMeta)which will hold json object and a Class that contains dictionary in the viewmodel , in the creation of the item, you could add section for the user defined data , so when viewing the data just parse the data in the column to the class that contains the dictionary – kaarimtareek Jan 04 '22 at 07:55

0 Answers0