ASP.NET core EF - Upload image varbinary to sql

Viewed 23

I'm with a issue with my code. Im building a rather simple web app ASP.NET Core With EF6 and Razor pages, and the objective is to be my final project on college. Deadline is near xD. Straight to the point - I can't make the image to be uploaded and then stored on my DB (SQL). Tried every tutorial available but coundn't make it work. A pasted part of the code, thanks in advance. Logotipo datatype on SQL is varbinary.

Model:

Column("inicio", TypeName = "date")]
    [DataType(DataType.Date)]
    public DateTime? Inicio { get; set; }
    [Column("area")]
    [StringLength(120)]
    public string? Area { get; set; }
    [Column("logotipo")]
    public byte[]? Logotipo { get; set; }

    [InverseProperty("IdmarcaNavigation")]
    public virtual ICollection<Modelo> Modelos { get; set; }
}

CreateView:

  </div>

         <div class="form-group">
            <label asp-for="Fabricante.Logotipo" class="control-label"></label>
            <input asp-for="Fabricante.Logotipo" class="form-control" />
            <span asp-validation-for="Fabricante.Logotipo" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>

Controller:

{
    private readonly DelianTec.Data.deptecContext _context;
    private IWebHostEnvironment _environment;
   

    public CreateModel(DelianTec.Data.deptecContext context, IWebHostEnvironment environment)
    {
        _context = context;
        _environment = environment;
    }



    public IActionResult OnGet()
    {
        return Page();
    }


   

    [BindProperty]
    public Fabricante Fabricante { get; set; }
    

    // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
    public async Task<IActionResult> OnPostAsync( )
    {
        
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.Fabricantes.Add(Fabricante);
        await _context.SaveChangesAsync();

        return RedirectToPage("./Index");
    }
0 Answers
Related