using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using EnVisage.Code; namespace EnVisage.Models { public class ViewModel : IBaseModel { public Guid Id { get; set; } [Required] [MaxLength(100)] [Display(Name="Name")] public string Name { get; set; } [Display(Name = "Number of Teams")] public int TeamCount { get; set; } public string backController{ get; set; } public string backAction { get; set; } public List ViewTeams { get { var dbContext = new EnVisageEntities(); var teams = (from c in dbContext.Teams join tv in dbContext.Team2View on c.Id equals tv.TeamId where tv.ViewId == Id select c).ToList(); var result = teams.Select(team => (TeamModel)team).ToList(); return result.OrderBy(x => x.Name).ToList(); } } [Required] [UIHint("MultipleSelect")] [Display(Name = "Watchers")] public IList UserId { get; set; } [Required] [UIHint("MultipleSelect")] [Display(Name = "Teams")] public IList TeamId { get; set; } public IEnumerable Users { get { EnVisageEntities DbContext = new EnVisageEntities(); return DbContext.AspNetUsers; } } /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator ViewModel(View obj) { if (obj == null) return null; var model = new ViewModel { Id = obj.Id, Name = obj.Name, TeamCount = obj.Team2View.Count }; model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(View dbObj) { if (dbObj == null) throw new ArgumentNullException(); dbObj.Name = Name; } } /// /// An UI representation of client to be displayed as list items /// public class ViewListModel { public Guid Id { get; set; } public string Name { get; set; } } }