C
C#5mo 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
Al3mid3xOP5mo ago
here is my Product class:
No description
Omnissiah
Omnissiah5mo ago
just a text phrase describing the error would be nice
Al3mid3x
Al3mid3xOP5mo 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
Al3mid3xOP5mo ago
Before Pressing Create Button :
No description
Al3mid3x
Al3mid3xOP5mo ago
after:
No description
Al3mid3x
Al3mid3xOP5mo ago
output
No description
Klodious
Klodious5mo 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
Al3mid3xOP5mo ago
ye it says the value of the IFormFile in the controller class is null
Klodious
Klodious5mo ago
you can make the image nullable by adding '?' after the image type
Al3mid3x
Al3mid3xOP5mo 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
Klodious5mo ago
use an image of type "IFormFile" instead of a byte array then you can cast it to store it
Al3mid3x
Al3mid3xOP5mo ago
but how can i load it in the view ?
Klodious
Klodious5mo ago
sorry, I thought the problem occurs only when you submit the form with an empty image
Al3mid3x
Al3mid3xOP5mo 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
Klodious5mo 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
Al3mid3xOP5mo 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
Klodious5mo ago
I noticed you are missing the input HTML element for the image?
Al3mid3x
Al3mid3xOP5mo 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
Klodious5mo ago
this is correct yeah is it still null?
Al3mid3x
Al3mid3xOP5mo ago
yes
Klodious
Klodious5mo ago
can you share with me the latest create.cshmtl and the product.cs
Al3mid3x
Al3mid3xOP5mo ago
No description
Al3mid3x
Al3mid3xOP5mo ago
No description
Al3mid3x
Al3mid3xOP5mo ago
and here is the Controller method
Al3mid3x
Al3mid3xOP5mo ago
No description
Klodious
Klodious5mo 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
Al3mid3xOP5mo 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
sibber5mo ago
mark the props are required in c#
Klodious
Klodious5mo 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
sibber5mo 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
Al3mid3xOP5mo ago
aight how didn't understand
sibber
sibber5mo ago
public required int Price { get; set; }
Al3mid3x
Al3mid3xOP5mo ago
oh ok
sibber
sibber5mo ago
range attribute
Al3mid3x
Al3mid3xOP5mo ago
oh
sibber
sibber5mo ago
your min value is set to 0
Al3mid3x
Al3mid3xOP5mo ago
i see thanks
sibber
sibber5mo ago
also no need for the {} its confusing it could make it seem like a bug
Al3mid3x
Al3mid3xOP5mo ago
No description
Al3mid3x
Al3mid3xOP5mo ago
d1 well i just want to to work tbh idc how would it look like
Klodious
Klodious5mo 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
Al3mid3xOP5mo ago
doing these leads me into another problem in the GET controller method "Create"
Al3mid3x
Al3mid3xOP5mo ago
No description
sibber
sibber5mo ago
yeah because you're not setting required properties
Al3mid3x
Al3mid3xOP5mo ago
no i think it would be easier to use the IFormFile
Klodious
Klodious5mo ago
set it with a default value
Al3mid3x
Al3mid3xOP5mo ago
I'm creating a new product it should set the required properties from the view
sibber
sibber5mo ago
Product doesn't know about any view
Al3mid3x
Al3mid3xOP5mo ago
then how am i gonna do it ? should i put empty values into it ? somthing like this
Al3mid3x
Al3mid3xOP5mo ago
No description
sibber
sibber5mo 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
Al3mid3xOP5mo 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
Al3mid3xOP5mo ago
No description
Al3mid3x
Al3mid3xOP5mo ago
No description
Klodious
Klodious5mo ago
remove the required keyword it will make you change maythings just use the required attribute it doesn't matter that much
Al3mid3x
Al3mid3xOP5mo ago
its working just fine finally
Al3mid3x
Al3mid3xOP5mo ago
No description
Klodious
Klodious5mo ago
the problem was with binding the image
Al3mid3x
Al3mid3xOP5mo ago
yes
Klodious
Klodious5mo ago
where are you from
Al3mid3x
Al3mid3xOP5mo ago
Jordan
Klodious
Klodious5mo ago
your name seems like an arabic name
Al3mid3x
Al3mid3xOP5mo ago
ye i'm actually arab
Klodious
Klodious5mo ago
ahln, I'm from Egypt
Al3mid3x
Al3mid3xOP5mo ago
oh really i'ma add you accept
Klodious
Klodious5mo ago
okay
Al3mid3x
Al3mid3xOP5mo ago
why didn't u tell me earlier it would've been easier for me to explain my problem xd
Klodious
Klodious5mo ago
I didn't read your name till 2mins ago
Al3mid3x
Al3mid3xOP5mo ago
thats ok habibi it was a pleasure meeting you 😄
Klodious
Klodious5mo ago
if you needed anything you can DM me
Al3mid3x
Al3mid3xOP5mo 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
Al3mid3xOP5mo ago
No description
Al3mid3x
Al3mid3xOP5mo ago
No description
Al3mid3x
Al3mid3xOP5mo ago
No description
Want results from more Discord servers?
Add your server