C
C#2w ago
Al3mid3x

✅ 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
Al3mid3x
Al3mid3x2w ago
here is my Product class:
No description
ffmpeg -i me -f null -
just a text phrase describing the error would be nice
Al3mid3x
Al3mid3x2w ago
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
Al3mid3x
Al3mid3x2w ago
Before Pressing Create Button :
No description
Al3mid3x
Al3mid3x2w ago
after:
No description
Al3mid3x
Al3mid3x2w ago
output
No description
Klodious
Klodious2w ago
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
Al3mid3x
Al3mid3x2w ago
ye it says the value of the IFormFile in the controller class is null
Klodious
Klodious2w ago
you can make the image nullable by adding '?' after the image type
Al3mid3x
Al3mid3x2w ago
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
Klodious
Klodious2w ago
use an image of type "IFormFile" instead of a byte array then you can cast it to store it
Al3mid3x
Al3mid3x2w ago
but how can i load it in the view ?
Klodious
Klodious2w ago
sorry, I thought the problem occurs only when you submit the form with an empty image
Al3mid3x
Al3mid3x2w ago
i was using this as i saw somewhere on stackoverflow <img id="productImage" alt="Product Image" src="data:image/*;base64,@(Convert.ToBase64String(Model.Image))"
Klodious
Klodious2w ago
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?
Al3mid3x
Al3mid3x2w ago
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
Klodious
Klodious2w ago
I noticed you are missing the input HTML element for the image?
Al3mid3x
Al3mid3x2w ago
this one ?
<input type="file"
asp-for="Image"
accept="image/*"
class="custom-file-input"
onchange="document.getElementById('productImage').src = window.URL.createObjectURL(this.files[0])" />
<input type="file"
asp-for="Image"
accept="image/*"
class="custom-file-input"
onchange="document.getElementById('productImage').src = window.URL.createObjectURL(this.files[0])" />
Klodious
Klodious2w ago
this is correct yeah is it still null?
Al3mid3x
Al3mid3x2w ago
yes
Klodious
Klodious2w ago
can you share with me the latest create.cshmtl and the product.cs
Al3mid3x
Al3mid3x2w ago
No description
Al3mid3x
Al3mid3x2w ago
No description
Al3mid3x
Al3mid3x2w ago
and here is the Controller method
Al3mid3x
Al3mid3x2w ago
No description
Klodious
Klodious2w ago
I don't understand why are you using the file parameter in the controller and you have a file property in the product
Al3mid3x
Al3mid3x2w ago
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
sibber
sibber2w ago
mark the props are required in c#
Klodious
Klodious2w ago
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
sibber
sibber2w ago
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
Al3mid3x
Al3mid3x2w ago
aight how didn't understand
sibber
sibber2w ago
public required int Price { get; set; }
Al3mid3x
Al3mid3x2w ago
oh ok
sibber
sibber2w ago
range attribute
Al3mid3x
Al3mid3x2w ago
oh
sibber
sibber2w ago
your min value is set to 0
Al3mid3x
Al3mid3x2w ago
i see thanks
sibber
sibber2w ago
also no need for the {} its confusing it could make it seem like a bug
Al3mid3x
Al3mid3x2w ago
No description
Al3mid3x
Al3mid3x2w ago
d1 well i just want to to work tbh idc how would it look like
Klodious
Klodious2w ago
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
Al3mid3x
Al3mid3x2w ago
doing these leads me into another problem in the GET controller method "Create"
Al3mid3x
Al3mid3x2w ago
No description
sibber
sibber2w ago
yeah because you're not setting required properties
Al3mid3x
Al3mid3x2w ago
no i think it would be easier to use the IFormFile
Klodious
Klodious2w ago
set it with a default value
Al3mid3x
Al3mid3x2w ago
I'm creating a new product it should set the required properties from the view
sibber
sibber2w ago
Product doesn't know about any view
Al3mid3x
Al3mid3x2w ago
then how am i gonna do it ? should i put empty values into it ? somthing like this
Al3mid3x
Al3mid3x2w ago
No description
sibber
sibber2w ago
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
Al3mid3x
Al3mid3x2w ago
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
No description
Al3mid3x
Al3mid3x2w ago
No description
Al3mid3x
Al3mid3x2w ago
No description
Klodious
Klodious2w ago
remove the required keyword it will make you change maythings just use the required attribute it doesn't matter that much
Al3mid3x
Al3mid3x2w ago
its working just fine finally
Al3mid3x
Al3mid3x2w ago
No description
Klodious
Klodious2w ago
the problem was with binding the image
Al3mid3x
Al3mid3x2w ago
yes
Klodious
Klodious2w ago
where are you from
Al3mid3x
Al3mid3x2w ago
Jordan
Klodious
Klodious2w ago
your name seems like an arabic name
Al3mid3x
Al3mid3x2w ago
ye i'm actually arab
Klodious
Klodious2w ago
ahln, I'm from Egypt
Al3mid3x
Al3mid3x2w ago
oh really i'ma add you accept
Klodious
Klodious2w ago
okay
Al3mid3x
Al3mid3x2w ago
why didn't u tell me earlier it would've been easier for me to explain my problem xd
Klodious
Klodious2w ago
I didn't read your name till 2mins ago
Al3mid3x
Al3mid3x2w ago
thats ok habibi it was a pleasure meeting you 😄
Klodious
Klodious2w ago
if you needed anything you can DM me
Al3mid3x
Al3mid3x2w ago
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
Al3mid3x
Al3mid3x2w ago
No description
Al3mid3x
Al3mid3x2w ago
No description
Al3mid3x
Al3mid3x2w ago
No description