0

What I'm trying to do is to create a form in asp.net - MVC which takes the input data from form and inserts it into the database. All the fields from the form are correctly taken from the form,except the image which always comes null.

I have tried to change the file in the model to have the type httppostedfilebase,it did not worked. I also added enctype = multipart/form-data in the form and I added a name for the input file.All the other fields in the form are correctly send to action in controller,except for the image file.

This is the view code: https://pastebin.com/H1TrBV2x

This is the controller code : https://pastebin.com/0LPMcwJc

This is the model:

public class Product
    {
        public string Name { get; set; }
        public int Price { get; set; }
        public string Description { get; set; }
        public string Category { get; set; }    
        public Image Picture { get; set; }
    }
Murat Kılınç
  • 175
  • 3
  • 16
Daniel
  • 261
  • 3
  • 18

1 Answers1

1

You cannot post and Image class directly like you are doing in the example. Change that back to HttpPostedFileBase

Secondly, have you renamed your input tag for your model ?

This is what I see,

<div class="form-group">
        @Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="file" id="file"/><br><br>                    
        </div>
    </div>

change it to this (Notice the name="Picture" )

<div class="form-group">
        @Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="Picture" id="file"/><br><br>                    
        </div>
    </div>

Please update the example for what you have changed.

zetawars
  • 1,023
  • 1
  • 12
  • 27
  • Thanks for the answer.Do you know any method to convert System.Drawing.Image to HttpPostedFileBase ? – Daniel Jan 01 '20 at 16:23
  • this can probably help you https://stackoverflow.com/questions/1171696/how-do-you-convert-a-httppostedfilebase-to-an-image – zetawars Jan 01 '20 at 19:40