'Image is not added / read in image upload field (asp.net)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using eCom.Data;
using eCom.Models;
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
namespace eCom.Areas.Admin.Controllers
{
[Area("Admin")]
public class ProductController : Controller
{
private ApplicationDbContext _db;
private IHostingEnvironment _he;
public ProductController(ApplicationDbContext db, IHostingEnvironment he)
{
_db = db;
_he = he;
}
public IActionResult Index()
{
return View(_db.Products.Include(c => c.ProductTypes).Include(f => f.SpecialTag).ToList());
}
//POST Index action method
[HttpPost]
public IActionResult Index(decimal? lowAmount, decimal? largeAmount)
{
var products = _db.Products.Include(c => c.ProductTypes).Include(c => c.SpecialTag)
.Where(c => c.Price >= lowAmount && c.Price <= largeAmount).ToList();
if (lowAmount == null || largeAmount == null)
{
products = _db.Products.Include(c => c.ProductTypes).Include(c => c.SpecialTag).ToList();
}
return View(products);
}
//Get Create method
public IActionResult Create()
{
ViewData["productTypeId"] = new SelectList(_db.ProductTypes.ToList(), "Id", "ProductType");
ViewData["TagId"] = new SelectList(_db.SpecialTag.ToList(), "Id", "Tag");
return View();
}
//Post Create method
[HttpPost]
public async Task<IActionResult> Create(Products products, IFormFile image)
{
if (ModelState.IsValid)
{
/*var searchProduct = _db.Products.FirstOrDefault(c => c.Tag == products.Tag);
if (searchProduct != null)
{
ViewBag.message = "This product is already exist";
ViewData["productTypeId"] = new SelectList(_db.ProductTypes.ToList(), "Id", "ProductType");
ViewData["TagId"] = new SelectList(_db.SpecialTag.ToList(), "Id", "Tag");
return View(products);
}*/
if (image != null)
{
var name = Path.Combine(_he.WebRootPath + "/Images", Path.GetFileName(image.FileName));
await image.CopyToAsync(new FileStream(name, FileMode.Create));
products.Image = "Images/" + image.FileName;
}
/*if (image == null)
{
products.Image = "Images/noimage.PNG";
}*/
_db.Products.Add(products);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(products);
}
I'm trying to make a website with asp.net. I'm a beginner. the theme of this website is the development of an online store with various products. everything went well until I had to create the first product (which, of course, also needs an image) I don't get any errors from the compiler, I don't understand what the problem is. below is the specific piece of code for creating a new product. after completing all the fields and adding an image click "save" and I receive this: https://imgur.com/qwpnwNJ below is also the part of the Create View for Products
<h2 class="text-info">Add New Product</h2>
<form asp-action="Create" method="post" enctype="multipart/form-data">
<div class="p-4 rounded border">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<h3>@ViewBag.message</h3>
<div class="form-group row">
<div class="col-2">
<label asp-for="Tag">Nume</label>
</div>
<div class="col-5">
<input asp-for="Tag" class="form-control" />
</div>
<span asp-validation-for="Tag" class="text-danger"></span>
</div>
<div class="form-group row">
<div class="col-2">
<label asp-for="Price"></label>
</div>
<div class="col-5">
<input asp-for="Price" class="form-control" />
</div>
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<div class="form-group row">
<div class="col-2">
<label asp-for="GetImage("></label>
</div>
<div class="col-5">
<input asp-for="GetImage()" class="form-control" type="file" />
</div>
<span asp-validation-for="GetImage()" class="text-danger"></span>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
