File input returning a null value ASP.NET MVC

I have a begin form on a view that contains a file input, but when I try pass the input value to my controller action it returns a null value.

Code in view:

@using (Html.BeginForm("DoCreate", "Admin", FormMethod.Post))
{
    <label>Description:</label>
    @Html.TextBox("Description", null, new { @class = "form-control", @type = "text", required = "required" });
    <label>Price:</label>
    @Html.TextBox("Price", null, new { @class = "form-control", @type = "number", required = "required" });
    <label>Quantity:</label>
    @Html.TextBox("Quantity", null, new { @class = "form-control", @type = "number", required = "required" });
    <label>Image:</label>
    @Html.TextBox("Image", null, new { @class = "form-control", @type = "file", required = "required" });
    <label>Product Type:</label>
    @Html.DropDownList("TypeID", new SelectList(Model, "ID", "TypeName"), "Select Product Type", new { @class = "form-control", required = "required" });

    <button class="btn btn-success" type="submit">Add to Table</button>
}

Code in controller:
[HttpPost]
public ActionResult DoCreate(string Description, float Price, int Quantity, HttpPostedFileBase Image, int TypeID)
{
    return View();
}

If insert a breakpoint by the action, all the other variables have the correct values but the Image returns null, I am not sure why this is, any help would be much appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Please check this

You need to just add htmlAttribute in form new { enctype = "multipart/form-data"}

@using (Html.BeginForm("DoCreate", "Admin", FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    <label>Description:</label>
    @Html.TextBox("Description", null, new { @class = "form-control", @type = "text", required = "required" });
    <label>Price:</label>
    @Html.TextBox("Price", null, new { @class = "form-control", @type = "number", required = "required" });
    <label>Quantity:</label>
    @Html.TextBox("Quantity", null, new { @class = "form-control", @type = "number", required = "required" });
    <label>Image:</label>
    @Html.TextBox("Image", null, new { @class = "form-control", @type = "file", required = "required" });
    <label>Product Type:</label>
    @Html.DropDownList("TypeID", new SelectList(Model, "ID", "TypeName"), "Select Product Type", new { @class = "form-control", required = "required" });

    <button class="btn btn-success" type="submit">Add to Table</button>
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x