I have 2 Models. Organization and Site. A org can have many sites but a site can only have 1 org. I have been able to successfully create a page that you can create an org with its primary site and all saves fine into the database.
What id like to do is have a page that shows all the sites for an organization. Id like the url to be something like ~/Organizations/6/Sites. and to see info on a specific site, the url should read ~/organizations/6/sites/2
How would i go about achieving this? Can anyone point me in the right direction. My understanding is that it would be done within the endpoints.MapControllerRoute section under the startup.cs file.
Below are the 2 models and the view action for the org and the viewsites action for the sites which currently both reside in the orgcontroller
public class Organization:BaseEntity
{
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "Logo")]
public string Logo { get; set; }
[Required]
[Display(Name = "Type")]
public OrganizationType Type { get; set; }
[DataType(DataType.DateTime)]
public DateTime CreatedDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime ModifiedDate { get; set; }
public int Demo { get; set; }
[Required]
public bool Active { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
public virtual ICollection<Site> Sites { get; set; }
}
public class Site : BaseEntity
{
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "Address")]
public string FullAddress
{
get
{
return StreetNumber + " " + StreetAddress + " " + Suburb + " " + State + " " + PostCode + " " + Country;
}
}
[Required]
[Display(Name = "Street Number")]
public string StreetNumber { get; set; }
[Required]
[Display(Name = "Street Address")]
public string StreetAddress { get; set; }
[Required]
[DataType(DataType.PostalCode)]
[Display(Name = "Postcode")]
public string PostCode { get; set; }
[Required]
[Display(Name = "Suburb")]
public string Suburb { get; set; }
[Required]
[Display(Name = "State")]
public string State { get; set; }
[Required]
[Display(Name = "Country")]
public string Country { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone Number")]
public string PhoneNumber { get; set; }
[DataType(DataType.DateTime)]
public DateTime CreatedDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime ModifiedDate { get; set; }
[Required]
public bool IsPrimary { get; set; }
public int Demo { get; set; }
[Required]
public bool Active { get; set; }
[Display(Name = "Image")]
public string Image { get; set; }
public virtual Organization Organization { get; set; }
}
// GET: Organizations/View/5
public async Task<IActionResult> View(Guid? id)
{
if (id == null)
{
return NotFound();
}
var organization = _context.Organizations.Include("Sites").Include("Contacts").FirstOrDefault(x=> x.ID == id);
ViewBag.SiteCount = organization.Sites.Count;
ViewBag.ContactCount = organization.Contacts.Count;
if (organization == null)
{
return NotFound();
}
return View(organization);
}
// GET: Organizations/View/5
public async Task<IActionResult> ViewSites(Guid? id)
{
if (id == null)
{
return NotFound();
}
List<Site> sites = _context.Organizations.Include("Sites").Where(x=>x.ID == id).ToList().FirstOrDefault().Sites.ToList();
return View(sites);
}
