Images for the Asp.Net MVC Product

Asked

Viewed 107 times

0

I have a problem, I don’t know how to save more than one image for a product. For example, an X product has a Highlight image (which is the one that will appear in the window), and 4 more images, which will appear on the product details page. I am trying to save the images in a folder, and save only the path of these images in the database (Good Practices). save only one image in the folder and the URL, in the bank I can, is not difficult, now save more than one image I can’t.

My Image Class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace E_Commerce.Models.Loja
{
    public class Imagem
    {
        [Key]
        public int Id { get; set; }
        [NotMapped]
        public HttpPostedFileBase Arquivo { get; set; }
        public string UrlDaImagem { get; set; }
    }
}

I know that in my product I have to reference the image class. But I do not know how to save these images to the product. I searched on the internet, and everything I thought was examples to save only one image in the folder, and the URL, in the bank, I also found, an example of uploading multiple files, however, did not save the way in the bank. (Macorrati)

My Product Class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace E_Commerce.Models
{
    public class ProdutoTeste
    {

        public ProdutoTeste()
        {
            this.Imagens = new List<Imagem>();
        }
        [Key]
        public int Id { get; set; }
        public string Descricao { get; set; }
        public decimal PrecoDeAtacado { get; set; }
        public decimal PrecoDeVarejo { get; set; }

        [ForeignKey("Categoria")]
        public int Idcategoria { get; set; }
        public virtual Categoria Categoria { get; set; }
        public virtual ICollection<Imagem> Imagens { get; set; }
    }
}

My Controller, saves only one image, but my protrude has more than one image. This controller below. is an example of how to save an image in the folder, and the URL in the database. the controller for the product I haven’t done yet, precisely because I don’t know how to save more than one image.

Controller:

 public ActionResult Create(FotoDaColecao fotoDaColecao)
        {

            string nomeDoArquivo = Path.GetFileNameWithoutExtension(fotoDaColecao.Arquivo.FileName);
            string extensao = Path.GetExtension(fotoDaColecao.Arquivo.FileName);
            nomeDoArquivo = nomeDoArquivo + DateTime.Now.ToString("yymmssffff") + extensao;
            fotoDaColecao.UrlDaImagem = "~/Imagens/Produtos/" + nomeDoArquivo;
            nomeDoArquivo = Path.Combine(Server.MapPath("~/Imagens/Produtos/"), nomeDoArquivo);
            fotoDaColecao.Arquivo.SaveAs(nomeDoArquivo);
            db.FotosDb.Add(fotoDaColecao);
            db.SaveChanges();
            return RedirectToAction("Index");



        }

In fact I want to do it this way: I will register product X, and at the time of registration I want to select your images, to save together. Do I need to create a controller for image? Can anyone help!!

  • Just get all the files from FormCollection...

  • I keep boycotting rsrsrsr. I’m layman when it comes to working with images.

  • include your view in the question, how are you doing the post for the controller

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.