예제 #1
0
        public async Task <IActionResult> HandleInfo(House house, string cancel)
        {
            //when click button Cancel, references to ViewNewsList View
            if (cancel == "Cancel")
            {
                List <House> ListNewsHouse = GetAllInfoHouse();
                ViewBag.List = ListNewsHouse;
                return(View("ViewNewsList"));
            }
            //when click button Ok to submit
            //check validation fields
            if (ModelState.IsValid)
            {
                //Id = 0 => Create new Entity
                if (house.Id == 0)
                {
                    _houseRepository.AddHouse(house);
                }
                else
                {
                    //Id != 0 => Update has Entity
                    _houseRepository.UpdateHouse(house);
                }

                int idImage = _houseRepository.GetMaxId();
                //save image and get image name
                for (int i = 0; i < house.ListImageFile.Count; i++)
                {
                    IFormFile image    = house.ListImageFile[i];
                    var       filePath = Path.Combine(_he.WebRootPath, "images", Path.GetFileName(image.FileName));
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await image.CopyToAsync(stream);

                        stream.Flush();
                    }
                    ImageNameOfHouse imageNameOfHouse = new ImageNameOfHouse
                    {
                        ImageName = image.FileName
                    };

                    _imageNameRepository.AddImageName(imageNameOfHouse, idImage);
                }
                //show news list => references to ViewNewsList View
                List <House> ListNewsHouse = GetAllInfoHouse();
                ViewBag.List = ListNewsHouse;
                return(View("ViewNewsList"));
            }
            //if Validation Model fail => return Index View and notify invalid data
            else
            {
                return(View("Index", house));
            }
        }
예제 #2
0
        public IActionResult AddHouse([FromBody] House house)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            house.HouseId = 0;

            _houseRepository.AddHouse(house);

            return(new JsonResult(house.HouseId));
        }
        public ActionResult Create(House house)
        {
            _houseRepository.AddHouse(house);

            return(RedirectToAction("Index"));
        }