C#C
C#3y ago
10 replies
Dasic

❔ How can I pass model data to my controller if I have another POCO class in the model?

Let's imagine that I have the classes:
public class Address
{
    public int Id { get; set; }
    public string? Street { get; set; }
    public string? BuildingNumber { get; set; }
    public string? Description { get; set; }
    public Entrance[]? Entrances { get; set; }
    public DateTime LastEdit { get; set; }
}

public class Entrance
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? Description { get; set; }
    public Floor[]? Floors { get; set; }
}

public class Floor
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? Description { get; set; }
}

How can I create a simple editor for these classes? I mean that my Create() method takes something like this:
public async Task<IActionResult> Create([Bind("Id,Street,BuildingNumber,Description,Entrances")] Address address) { ... }

Of course I cannot bind Entrances property because it is another class and it is an array. How can I do the trick? What should my controller accept and how can I do it in the view?
Was this page helpful?