public ActionResult AddProperty(PropertyViewModel viewModel) { if (ModelState.IsValid) { if (viewModel.PropertyImages.files != null) { var imageList = new List <PropertyImageViewModel>(); foreach (var image in viewModel.PropertyImages.files) { if (image != null) { var InputFileName = Path.GetFileName(image.FileName); var ServerSavePath = Path.Combine(Server.MapPath("~/UploadedFiles/") + InputFileName); //Save file to server folder image.SaveAs(ServerSavePath); PropertyImageViewModel img = new PropertyImageViewModel { Name = viewModel.PropertyName, Size = image.ContentLength, filePath = "~/UploadedFiles/" + InputFileName }; imageList.Add(img); } } viewModel.UserId = User.Identity.GetSecurityUserId(); _addProperty.Add(viewModel, imageList); } } return(RedirectToAction("DisplayUserProperty")); }
public async Task <IActionResult> AddImage(PropertyImageViewModel model) { if (ModelState.IsValid) { var path = string.Empty; if (model.ImageFile != null) { path = await _imageHelper.UploadImageAsync(model.ImageFile); } var propertyImage = new PropertyImage { ImageUrl = path, Property = await _dataContext.Properties.FindAsync(model.Id) }; _dataContext.PropertyImages.Add(propertyImage); await _dataContext.SaveChangesAsync(); return(RedirectToAction($"{nameof(DetailsProperty)}/{model.Id}")); } return(View(model)); }
public ActionResult GetPropertyInfoByImageId(int imageId) { var propertyModel = _getUserProperty.Get(imageId); List <PropertyImageViewModel> list = new List <PropertyImageViewModel>(); foreach (var m in propertyModel.Images) { var imageModel = new PropertyImageViewModel { filePath = m.filePath, Id = m.Id }; list.Add(imageModel); } PropertyViewModel model = new PropertyViewModel { Address = propertyModel.Address, Amenities = propertyModel.Amenities, Details = propertyModel.Details, City = propertyModel.City, PropertyName = propertyModel.PropertyName, State = propertyModel.State, Id = propertyModel.Id, University = propertyModel.University, Property = list }; return(View("_UpdateUserProperty", model)); }
public async Task <IActionResult> AddImage(PropertyImageViewModel model) { //Validamos si el modelo es válido if (ModelState.IsValid) { //creamos la variable path asumiendo que adiciono la imagen string path = string.Empty; //Imagefile=Iformfile donde se captura la imagen //Si hay una imagen if (model.ImageFile != null) { //Llamamos al método que creamos en la interface para subir la imagen //nos devuelve la ruta de como se va ha guradar en la bd path = await _imageHelper.UploadImageAsync(model.ImageFile); } //Creamos el objeto PropertyImage PropertyImage propertyImage = new PropertyImage { ImageUrl = path,//Es la ruta que nos devovlio del método en la interface //buscamos el objeto con el id, ya que desde el get no lo podemos enviar Property = await _dataContext.Properties.FindAsync(model.Id) }; //Finalmente guardamos en la coleccion de imagenes la propertyImage //retornamos al detailsProperty con el id de la propiedad _dataContext.PropertyImages.Add(propertyImage); await _dataContext.SaveChangesAsync(); return(RedirectToAction($"{nameof(DetailsProperty)}/{model.Id}")); } return(View(model)); }
public async Task <IActionResult> AddImage(int?id) { if (id == null) { return(NotFound()); } var property = await _dataContext.Properties.FindAsync(id.Value); if (property == null) { return(NotFound()); } var model = new PropertyImageViewModel { Id = property.Id }; return(View(model)); }
public async Task <IActionResult> AddImage(PropertyImageViewModel model) { if (ModelState.IsValid) { var path = string.Empty; if (model.ImageFile != null && model.ImageFile.Length > 0) { var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot\\images\\Properties", file); using (var stream = new FileStream(path, FileMode.Create)) { await model.ImageFile.CopyToAsync(stream); } path = $"~/images/Properties/{file}"; } var propertyImage = new PropertyImage { ImageUrl = path, Property = await _dataContext.Properties.FindAsync(model.Id) }; _dataContext.PropertyImages.Add(propertyImage); await _dataContext.SaveChangesAsync(); return(RedirectToAction("DetailsPropertyOwner", "Home", new { id = model.Id })); // return RedirectToAction($"{nameof(DetailsPropertyOwner)}/{model.Id}"); } return(View(model)); }
//Le mandamos el id //Verifico el estado del modelo //Busca la propiedad asignada a ese id //Si la encuentra tenemos que asignar al property los campos del proerty view model //Esto recupera los datos correspondientes de la imagen public async Task <IActionResult> AddImage(int?id) { if (id == null) { return(NotFound()); } var property = await _dataContext.Properties.FindAsync(id); if (property == null) { return(NotFound()); } //Hacemos esto para la indicar y guardar en el model el id de la propiedad var model = new PropertyImageViewModel { Id = property.Id, }; return(View(model)); }
public async Task <PropertyImageViewModel> GetImageById(int imageId) { var image = await _context.PropertyImages.FindAsync(imageId); if (image == null) { throw new RealEstateException($"Không thể tìm thấy hình: {imageId}"); } var viewModel = new PropertyImageViewModel() { Caption = image.Caption, DateCreated = image.DateCreated, FileSize = image.FileSize, Id = image.Id, LinkName = image.LinkName, IsDefault = image.IsDefault, PropertyId = image.PropertyId, SortOrder = image.SortOrder }; return(viewModel); }
//id de la propiedad public async Task <IActionResult> AddImage(int?id) { //id que no sea igual a null if (id == null) { return(NotFound()); } //se busca en la base de datos en la collecion propiedad por el id var property = await _dataContext.Properties.FindAsync(id.Value); if (property == null) { return(NotFound()); } // el modelo va hacer un nuevo propertyimageviewmodel var model = new PropertyImageViewModel { //se manda al modelo el id Id = property.Id }; return(View(model)); }
public async Task <IActionResult> AddImage(int?id) { //Validamos el id de la propiedad if (id == null) { return(NotFound()); } //Buscamos la propiedad Property property = await _dataContext.Properties.FindAsync(id.Value); if (property == null) { return(NotFound()); } //Creamos el modelo para enviar a la vista PropertyImageViewModel model = new PropertyImageViewModel { Id = property.Id }; return(View(model)); //En la vista el usuario selecciona la imagen y vuelve al post }