Example #1
0
 public SearchResultViewModel(Models.Data.Property p)
 {
     this.Id             = p.Id;
     this.DisplayText    = p.Name;
     this.DisplaySubText = p.Building + " " + p.Unit + " " + p.Street + " " + p.City;
     this.ResultType     = nameof(Property);
 }
        public ActionResult Create(CreatePropertyViewModel newProperty)
        {
            //Validate that required Fields are filled in
            if (!ModelState.IsValid)
            {
                return(View(newProperty));
            }

            //Create an instance of DBContext
            using (RateMyLandlordDbContext context = new RateMyLandlordDbContext())
            {
                //Make sure the new Property is Unique by comparing addresses.
                if (context.Properties.Any(row => row.Unit.Equals(newProperty.Unit)) &&
                    context.Properties.Any(row => row.Building.Equals(newProperty.Building)) &&
                    context.Properties.Any(row => row.Street.Equals(newProperty.Street)) &&
                    context.Properties.Any(row => row.City.Equals(newProperty.City)) &&
                    context.Properties.Any(row => row.Region.Equals(newProperty.Region)) &&
                    context.Properties.Any(row => row.Country.Equals(newProperty.Country)) &&
                    context.Properties.Any(row => row.ZipCode.Equals(newProperty.ZipCode))
                    )
                {
                    ModelState.AddModelError("", "This Property already exists.");
                    return(View());
                }
                //Create UserDTO
                Property newPropertyDTO = new Models.Data.Property()
                {
                    Name              = newProperty.Name,
                    Unit              = newProperty.Unit,
                    Building          = newProperty.Building,
                    Street            = newProperty.Street,
                    City              = newProperty.City,
                    Region            = newProperty.Region,
                    Country           = newProperty.Country,
                    ZipCode           = newProperty.ZipCode,
                    Rating            = newProperty.Rating,
                    RatingDescription = newProperty.RatingDescription,
                    Description       = newProperty.Description
                };
                //Add to context
                newPropertyDTO = context.Properties.Add(newPropertyDTO);
                // Save Changes
                context.SaveChanges();
            }

            //Redirect to Properties page.
            return(RedirectToAction("Index"));
        }