I am building a project using .NET 7 and have three models that are related to each other in Models and I don't know how to implement them both in the create controller and view also with
get
method.
The three classes are "
RealEstate
" model, "
RealEstateAgent
" model and "
RealEstateLocations
" model. My question is... will I create the
realestate
together with the
realestate
models and is this the best way to go? Another guess if I am to create the location in its separate controller, how am I going to link it up in the database.
<pre> public class RealEstate
{
public int Id { get; set; }
[Required]
[StringLength(100)]
public string Title { get; set; }
[Required]
[StringLength(255)]
public string Address { get; set; }
public DateTime DateEntered { get; set; }
[Required]
[DataType(DataType.Currency)]
[Display(Name = "Price")]
public decimal Price { get; set; }
[Display(Name = "Number of Bedrooms")]
public int Bedrooms { get; set; }
[Display(Name = "Number of Bathrooms")]
public int Bathrooms { get; set; }
[Display(Name = "Square Footage")]
public int SquareFootage { get; set; }
[Display(Name = "Year Built")]
public int YearBuilt { get; set; }
[StringLength(5000)]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[DataType(DataType.ImageUrl)]
[Display(Name = "Image")]
public string ImageUrl { get; set; }
public int RealEstateAgentId { get; set; }
public RealEstateAgent RealEstateAgent { get; set; }
public ICollection<RealEstateLocation>
RealEstateLocation { get; set; }
}
and the other model is below:
public class RealEstateLocation
{
public int Id { get; set; }
public string Address { get; set; }
public string Country {get;set;}
public string City { get;set;}
public string State { get; set; }
public string Phone { get; set; }
public string? Email { get; set; }
public string? Website { get; set; }
public int RealEstateId { get; set; }
public RealEstate RealEsate { get; set; }
}
and the last:
public class RealEstateAgent
{
public int Id { get; set; }
[Required]
[StringLength(500)]
[Display(Name = "Full Name")]
public string FullName { get; set; }
[StringLength(50)]
[Display(Name = "License Number")]
public string LicenseNumber { get; set; }
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone Number")]
public string PhoneNumber { get; set; }
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
[StringLength(5000)]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[DataType(DataType.ImageUrl)]
[Display(Name = "Image")]
public string ImageUrl { get; set; }
[Display(Name = "Years of Experience")]
public int YearsOfExpereince { get; set; }
[Display(Name = "Office Address")]
public string OfficeLocation { get; set; }
}
I was doing the
create
method in
RealEstate
controller, but I don't know how to implement the
RealEstate
create in it and also the location.
Please help! Thanks!
What I have tried:
public IActionResult GetRealestate(int rId)
{
var realestateDetails = from realestatei in _dbContext.RealEstates
where realestatei.Id == rId
select new
{
Id = realestatei.Id,
Title = realestatei.Title,
Address = realestatei.Address,
Price = realestatei.Price,
Badrooms = realestatei.Bedrooms,
Bathrooms = realestatei.Bathrooms,
SquareFootage =
realestatei.SquareFootage,
YearBuilt = realestatei.YearBuilt,
Description = realestatei.Description,
ImageUrl = realestatei.ImageUrl,
RestaurantLocation =
realestatei.RealEstateLocation.Select
(r => new
{
Id = r.Id,
Address = r.Address,
Country = r.Country,
City = r.City,
Phone = r.Phone,
State = r.State,
Email = r.Email,
Website = r.Website,
}),
};
var realestateDetailsViewModel =
realestateDetails.SingleOrDefault();
if (realestateDetailsViewModel == null)
{
return NotFound();
}
return Ok(realestateDetailsViewModel);
}