0

I am using ASP.NET MVC, I created two model classes, User and Address. I want to use them on the same view to allow a user to enter all the information on one page. The problem is that ASP.NET MVC allows to pass only one model. What is the best practice to resolve this situation?

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

    public string Route { get; set;}
    public string City { get; set; }

    public string State { get; set;  }

    public string PostalCode { get; set; }

    public string Coutry { get; set; }

    public int Adresstype { get; set; }
    public int UserId { get; set; }
    public virtual User User { get; set; }
}

public class User
{
    public User()
    {
        this.Ads = new HashSet<Ad>();
        this.Addresses = new HashSet<Address>();
    }

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

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public int PhoneNumber { get; set; }

    public virtual ICollection<Ad> Ads { get; set; }
    public virtual ICollection<Address> Addresses { get; set;}
}
YacineSM
  • 81
  • 3

1 Answers1

0

The idea is to create a view model for that exact view - and this is just a class where you can package anything you like - like one of each of your base classes:

public class ViewModel
{
    public User User { get; set; }
    public Address Address { get; set; }
} 

and then you pass this view model class into your view and you can easily handle values for both things - users and addresses.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459