RoboticOperatingBuddy
RoboticOperatingBuddy
CC#
Created by RoboticOperatingBuddy on 1/14/2024 in #help
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 will not be good enough in this case? I would either need picture data itself in order to throw a 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 '^^
7 replies
CC#
Created by RoboticOperatingBuddy on 1/13/2024 in #help
VS2022 errors out on a project cloned from Git
No description
17 replies