I have a Person class displayed below. In the view I need to display its properties, plus the DepartmentName that exists in a different Table. DepartmentId in the class below is a Foreign Key. I am using Linq to SQL. I am also display my PersonRepository and Controller code below. I need to display the DepartmentName in the Index View page and a dropdown list on the Edit View Page. I am new to MVC, so I am not sure how to do this. Any help is very much appreciated.
public class Person_Validation
{
[HiddenInput(DisplayValue = false)]
[ScaffoldColumn(false)]
public object PersonId { get; set; }
[HiddenInput(DisplayValue = false)]
[ScaffoldColumn(false)]
public object DepartmentId { get; set; }
[DisplayName("First Name")]
[Required(ErrorMessage = "First Name is required")]
[StringLength(50, ErrorMessage = "Frist Name cannot be more than 50 characters")]
public object FirstName { get; set; }
[DisplayName("Last Name")]
[Required(ErrorMessage = "Last Name is required")]
[StringLength(50, ErrorMessage = "Last Name cannot be more than 50 characters")]
public object LastName { get; set; }
[HiddenInput(DisplayValue = false)]
public object Active { get; set; }
[HiddenInput(DisplayValue = false)]
public object DateAdded { get; set; }
[HiddenInput(DisplayValue = false)]
public object DateDeleted { get; set; }
public IEnumerable<Department> departments { get; set; }
}
PersonRepository class:
public IList<Person> GetAllPersons()
{
//get all active people
var activePeople = from p in db.Persons
where p.Active == true
select p;
return activePeople.ToList();
}
Controller code:
public ActionResult Index(int? page)
{
const int pageSize = 25;
var persons = _repository.GetAllPersons();
var paginatedPersons = new PaginatedList<Person>(persons, page ?? 0, pageSize);
return View(paginatedPersons);
}