Esempio n. 1
0
        public IActionResult AddEditAnimal(int?id)
        {
            GoatCreateVM model = new GoatCreateVM();

            if (id.HasValue)
            {
                GoatRegistration feed = _repo.GoatRegistration.GetById(id.Value);


                if (feed != null)
                {
                    model = _mapper.Map <GoatRegistration, GoatCreateVM>(feed);
                }
            }
            //model.speciess = (_repo.Species.GetModel(), "id", "speciesName");
            //model.breeds = new SelectList(_repo.Breed.GetModel(), "id", "breedNameShort");
            //model.owners = new SelectList(_repo.OwnerKeeper.GetModel(), "id", "fullName");
            //model.farms = new SelectList(_repo.Farm.GetModel(), "id", "orgtanizationName");
            //model.keepers = new SelectList(_repo.keepers.GetModel(), "id", "fullName");
            //model.dams = new SelectList(_repo.AnimalRegistration.GetModel().Where(m=>m.gender=="Male"), "id", "earTagNo");
            //model.sires = new SelectList(_repo.AnimalRegistration.GetModel().Where(m => m.gender == "Female"), "id", "earTagNo");
            model.id = 0;

            model.GoatBreeds = new SelectList(_repo.GoatBreed.GetModel(), "Id", "BreedName");
            model.UpdatedAt  = DateTime.Now.Date;
            return(View(model));
        }
Esempio n. 2
0
        public ActionResult AddEditAnimal(int?id, GoatCreateVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bool isNew = !id.HasValue;
                    if (isNew)
                    {
                        model.UpdatedAt = DateTime.Now;
                        GoatRegistration animal = _mapper.Map <GoatCreateVM, GoatRegistration>(model);
                        animal.GoatBreedId = model.GoatBreedId;
                        model.GoatBreeds   = new SelectList(_repo.GoatBreed.GetModel(), "Id", "BreedName", animal.GoatBreedId);
                        animal.UpdatedBy   = "admin";

                        _repo.GoatRegistration.Insert(animal);
                        _repo.Save();
                        //var path = _host.WebRootPath + "\\images\\animal\\";

                        //if (!Directory.Exists(path))
                        //{
                        //    Directory.CreateDirectory(path);
                        //}
                        //if (file != null)
                        //{

                        //    var fileName = Path.Combine(path, animal.earTagNo + ".jpg");
                        //    file.CopyTo(new FileStream(fileName, FileMode.Create));
                        //}
                    }
                    else
                    {
                        //var config = new MapperConfiguration(cfg =>
                        //{
                        //    cfg.CreateMap<AnimalVM, AnimalRegistration>();
                        //});

                        //IMapper iMapper = config.CreateMapper();

                        GoatRegistration animal = _mapper.Map <GoatCreateVM, GoatRegistration>(model);
                        animal.GoatBreedId = model.GoatBreedId;
                        animal.UpdatedBy   = "admin";
                        animal.UpdatedAt   = DateTime.Now;
                        _repo.GoatRegistration.Update(animal);
                    }
                }
                else
                {
                    //model.speciess = new SelectList(_repo.Species.GetModel(), "id", "speciesName",model.speciesId);
                    //model.breeds = new SelectList(_repo.Breed.GetModel(), "id", "breedNameShort",model.breedId);
                    //model.owners = new SelectList(_repo.OwnerKeeper.GetModel(), "id", "fullName",model.ownerId);
                    //model.keepers = new SelectList(_repo.keepers.GetModel(), "id", "fullName",model.keeperId);
                    //model.farms = new SelectList(_repo.Farm.GetModel(), "id", "orgtanizationName",model.farmId);
                    //model.dams = new SelectList(_repo.AnimalRegistration.GetModel().Where(m => m.gender == "Male"), "id", "earTagNo",model.damId);
                    //model.sires = new SelectList(_repo.AnimalRegistration.GetModel().Where(m => m.gender == "Female"), "id", "earTagNo",model.sireId);
                    //model.declaredDate = DateTime.Now.Date;
                    model.GoatBreeds = new SelectList(_repo.GoatBreed.GetModel(), "Id", "BreedName");
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(RedirectToAction("Index"));
        }