0

I am trying to upload an image to save it to SQL Server Express.

This is my view:

@model Nettbutikk.Models.ProductsModel

@{
}

@Html.Raw(ViewBag.msg)

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <h1>Add New Product</h1>
    <form enctype="multipart/form-data">
        <p>
            <span>Product price<label>*</label></span>
            <br />
            @Html.EditorFor(ProductsModel => ProductsModel.ProductPrice, new { @htmlattributes = new { @class = "control-form" } })
        </p>

        <p>
            <span>Image<label>*</label></span>
            <br />
            @Html.TextBoxFor(ProductsModel => ProductsModel.ProductImage, new { type = "file" })
            
        </p>

        <p>
            <input type="submit" value="Add New Product" />
        </p>
    </form>
}

Here is my model class:

public class ProductsModel
{
    public Image ProductImage { get; set; }
    public int ProductPrice { get; set; }
    public int ProductId { get; set; }
}

And this is my controller method:

[HttpPost]
public IActionResult NewProduct(ProductsModel objProductModel)
{
    string conStr = "Data Source=.\\SQLEXPRESS; Initial Catalog=Nettbutikk; Integrated Security=True";

    SqlConnection con = new SqlConnection(conStr);
    con.Open();

    string query = "insert into products(price, picture) select " + objProductModel.ProductPrice + ", BulkColumn FROM Openrowset(Bulk '" + objProductModel.ProductImage + "', Single_Blob) as img";

    SqlCommand cmd = new SqlCommand(query, con);

    cmd.ExecuteNonQuery();
    con.Close();

    return View();
}

The problem is that objProductModel.ProductImage is null, which means the image is not sent to the controller. What am I doing wrong? Please help.

5
  • This question is similar to: image file not pass to controller show null value MVC. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 17, 2024 at 22:35
  • The type for Image in your model is not correct, and you need to add enctype="multipart/form-data to @using (Html.BeginForm()), and then get rid of the second form inside that form. Inspect the rendered HTML and you'll see that Html.BeginForm creates a form tag. Commented Dec 17, 2024 at 22:38
  • I did exactly the same, edited Image property to HttpPostedFileBase instead of Image. Added enctype="multipart/form-data to BeginForm() and then changed objProductModel.ProductImage to objProductModel.ImageFile inside Controller. But still null. Commented Dec 17, 2024 at 23:09
  • may be I should use something else than: @Html.TextBoxFor(ProductsModel => ProductsModel.ImageFile, new { type = "file" }) Commented Dec 17, 2024 at 23:15
  • You can verify what's in the POST payload by inspecting the Network tab of your browser's developer tools. If ProductImage is null / empty, you'll know there's a problem on your view / form. I've always used a regular HTML input tag with the type="file" attribute. If you do that, be sure to also include the name attribute so MVC maps it to your model property. The question in the thread linked above shows how the OP of that question has their input form field defined for the file upload Commented Dec 17, 2024 at 23:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.