✅ Image input field is required
Hey guys i've been facing this error even for a while now and i don't have any idea how to fix it
keep in mind i'm new to asp.net & web development in general
here is a video that explains everything
74 Replies
here is my Product class:
just a text phrase describing the error would be nice
when i press create after filling the inputs the input field for image removes the image (which is displaying by javascript) and validation says it is required even tho in my product class i didn't put a Required attribute
Before Pressing Create Button :
after:
output
I'm not sure, but try to put a breakpoint before the program checks the model validity, and see what is the value of image's byte array which is passed by the form, if it's null probably this is the cause, (the controller is trying to assign a null to a not nullable variable).
if that's the problem, making the image nullable in the product class will resolve the issue
ye it says the value of the IFormFile in the controller class is null
you can make the image nullable by adding '?' after the image type
well ye it did remove the validation message but how can i fix the file being null tho ?
what i'm trying to do is take a picture from the input and save it as a array of bytes in the database
use an image of type "IFormFile" instead of a byte array
then you can cast it to store it
but how can i load it in the view ?
sorry, I thought the problem occurs only when you submit the form with an empty image
i was using this as i saw somewhere on stackoverflow
<img id="productImage" alt="Product Image" src="data:image/*;base64,@(Convert.ToBase64String(Model.Image))"
yes, you can turn it into a base 64 string and assign it like this
or you can just store the images on the file system
which imo is better I hate storing images on the db
did this work for u?
1sec
well ModelState is still not valid
can you explain how to store it in if its actually easier
cuz its been pain in the ass trying to store it in db
I noticed you are missing the input HTML element for the image?
this one ?
this is correct yeah
is it still null?
yes
can you share with me the latest create.cshmtl and the product.cs
and here is the Controller method
I don't understand why are you using the file parameter in the controller and you have a file property in the product
i was following this tut https://stackoverflow.com/questions/42741170/how-to-save-images-to-database-using-asp-net-core
Stack Overflow
How to save Images to database using ASP.NET Core?
I am working on a small blog using ASP.NET Core(MVC 6) EF Visual Studio. I have trouble finding how to save images to a database. I have read about IFormfile but I do not really understand how to go
mark the props are required in c#
if you want to make it like this because the file property in the product class has a different type then you need to remove the "Image" from the binding
also your error message says the range should start at 1 but youre starting at 0?
also [Key] is set by default on properties named Id or ClassNameId
aight
how
didn't understand
public required int Price { get; set; }
oh ok
range attribute
oh
your min value is set to 0
i see
thanks
also no need for the {} its confusing it could make it seem like a bug
d1
well i just want to to work tbh idc how would it look like
the implementation is confusing, if you want to make the file property in tthe product an array of bytes, then don't bind it, if no you will need to change many things
doing these leads me into another problem in the GET controller method "Create"
yeah because you're not setting required properties
no i think it would be easier to use the IFormFile
set it with a default value
I'm creating a new product it should set the required properties from the view
Product doesn't know about any view
then how am i gonna do it ?
should i put empty values into it ?
somthing like this
I've never done mvc but you need to get the data from the view somehow
i mean that just create a default product everytime
i don't know if that's what you want
i want when the user clicks on "Add Product" Button leads him to this page and after clicking on "Create" in this view it will create a new product and store it into database then redirect the user to the products page
remove the required keyword it will make you change maythings
just use the required attribute
it doesn't matter that much
its working just fine
finally
the problem was with binding the image
yes
where are you from
Jordan
your name seems like an arabic name
ye i'm actually arab
ahln, I'm from Egypt
oh really
i'ma add you accept
okay
why didn't u tell me earlier it would've been easier for me to explain my problem xd
I didn't read your name till 2mins ago
thats ok habibi
it was a pleasure meeting you 😄
if you needed anything you can DM me
Thank you all for helping i've been stuck on this one for a long time
i'll send the latest update of the code if anybody else saw this post would know how i did fix it