C
C#6mo ago
Al3mid3x

✅ NullReferenceException for a array of bytes (image)

hey everybody i'm new to ASP.NET i was doing a small ecommerce project and i want to add a default picture for the product if not exists but i keep getting null error on the if statement that checks if its null. here is a picture of the error keeps appearing in visual studio
No description
9 Replies
canton7
canton76mo ago
Model is null
Al3mid3x
Al3mid3xOP6mo ago
well i'm trying to create a new product (which is the model used for this page)
@model Giftos.Entities.Product
@model Giftos.Entities.Product
and here is my Product class
c#
using System.ComponentModel.DataAnnotations;

namespace Giftos.Entities
{
public class Product
{
[Key]
public Guid Id { get; set; }

[Required(ErrorMessage = "This field is required.")]
public string Name { get; set; }

[Required(ErrorMessage = "This field is required.")]
public string Description { get; set; }

[Required(ErrorMessage = "This field is required.")]
public int Stock { get; set; }

[Required(ErrorMessage = "This field is required.")]
public int Price { get; set; }
public byte[] Image { get; set; }




public Product(string name, string description, int price, int stock)
{
Name = name;
Description = description;
Price = price;
Stock = stock;
}

public Product() { }
}
}
c#
using System.ComponentModel.DataAnnotations;

namespace Giftos.Entities
{
public class Product
{
[Key]
public Guid Id { get; set; }

[Required(ErrorMessage = "This field is required.")]
public string Name { get; set; }

[Required(ErrorMessage = "This field is required.")]
public string Description { get; set; }

[Required(ErrorMessage = "This field is required.")]
public int Stock { get; set; }

[Required(ErrorMessage = "This field is required.")]
public int Price { get; set; }
public byte[] Image { get; set; }




public Product(string name, string description, int price, int stock)
{
Name = name;
Description = description;
Price = price;
Stock = stock;
}

public Product() { }
}
}
Al3mid3x
Al3mid3xOP6mo ago
ProductsController class
Kouhai
Kouhai6mo ago
You need to provide a model instance to the view
Al3mid3x
Al3mid3xOP6mo ago
can you tell me how to do it please as i said i'm new to asp.net
Kouhai
Kouhai6mo ago
// GET: Products/Create
public IActionResult Create()
{
return View(new Product());
}
// GET: Products/Create
public IActionResult Create()
{
return View(new Product());
}
Should do the trick
Al3mid3x
Al3mid3xOP6mo ago
Ye it did thank you i have another thing i need help with should i open another post for it ?@canton7@Kouhai
canton7
canton76mo ago
Yes $close
MODiX
MODiX6mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server