© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
6 replies
RoboticOperatingBuddy

Add a file upload to an ASP.NET project

Hi, I've kind of messed up and mistook a deadline on a project for anther, and suddenly found myself very behind on work, and I will need some help.

We have a web store project which lets you add products as a seller and then buy them as a buyer. This works fine, but we want to add pictures of the products aswell.

This is in our controller:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,Description,Price")] Product product)
{
    product.SellerId = _userManager.GetUserId(User);

    if (ModelState.IsValid)
    {
        _context.Add(product);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View(product);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,Description,Price")] Product product)
{
    product.SellerId = _userManager.GetUserId(User);

    if (ModelState.IsValid)
    {
        _context.Add(product);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View(product);
}


This is our model:
public class Product 
{
    public int Id { get; set; }
    [Display(Name = "Nazwa")]
    public string Name { get; set; }
    [Display(Name = "Opis")]
    public string Description { get; set; }
    //public int CategoryId { get; set; }
    [Display(Name = "Cena")]
    public decimal Price { get; set; }

    [ForeignKey(nameof(Seller))]
    [ValidateNever]
    public string SellerId { get; set; }
    [ValidateNever]
    public virtual ApplicationUser Seller { get; set; }
    
    public int PictureId { get; set; }
}
public class Product 
{
    public int Id { get; set; }
    [Display(Name = "Nazwa")]
    public string Name { get; set; }
    [Display(Name = "Opis")]
    public string Description { get; set; }
    //public int CategoryId { get; set; }
    [Display(Name = "Cena")]
    public decimal Price { get; set; }

    [ForeignKey(nameof(Seller))]
    [ValidateNever]
    public string SellerId { get; set; }
    [ValidateNever]
    public virtual ApplicationUser Seller { get; set; }
    
    public int PictureId { get; set; }
}


Now, I'm somewhat sure
PictureId
PictureId
will not be good enough in this case? I would either need picture data itself in order to throw a
data:image
data:image
sort of thing into the page, or a path to the picture.

I am attempting to follow https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0#upload-small-files-with-buffered-model-binding-to-a-database, but as with most Microsoft guides, I sadly find it too technical to really follow. I get lost easily with what part of the code is what '^^
Upload files in ASP.NET Core
How to use model binding and streaming to upload files in ASP.NET Core MVC.
Upload files in ASP.NET Core
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

File upload ASP .net 6
C#CC# / help
3y ago
how to upload a file in .net project
C#CC# / help
3y ago
How to make upload file in ASP.NET Core?
C#CC# / help
2y ago