0

I want to upload an image into a database (ShoppingItems) OR save the image to a folder in my project and insert the path of the image into the db. Can anybody help? This is my code (view):

@using (Html.BeginForm("Index3", "Upload", FormMethod.Post))
{
    @Html.TextBoxFor(x => x.Itemname, new { @class = "form-control", placeholder = "Item name", required = "required" })
    <br />

    @Html.TextBoxFor(x => x.Price, new { @class = "form-control", placeholder = "Item price", required = "required" })
    <br />

    @Html.TextBoxFor(x => x.Quantity, new { @class = "form-control", placeholder = "Item quantity"})
    <br />

    @Html.TextBoxFor(x => x.AuthorIdentity, new { @class = "form-control", placeholder = "Username", required = "required" })
    <br />

    // THIS IS WHERE MY IMAGE UPLOAD SHOULD BE  
    <br />

    @Html.TextBoxFor(x => x.Category, new { @class = "form-control", placeholder = "Item category", required = "required" })
    <br />

    @Html.TextAreaFor(x => x.Description, new { @class = "form-control", placeholder = "Item description", required = "required" })
    <br />

    <input type="submit" class="btn btn-danger" value="Add" />
}

Controller:

public ActionResult Index3(ShoppingItem formModel);
{
    using (var ctx = new GikGamerModelDataContext())
    {
        if (formModel == null)
            return View();

        ctx.ShoppingItems.InsertOnSubmit(formModel);
        ctx.SubmitChanges();
    }

    return View();
}

My upload index (Index3) just shows text that says that your upload was successful or unsuccessful so I haven't added it :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
GikGamer
  • 11
  • 3

1 Answers1

0

in the form in order to upload you have to specify the attribute enctype : with the value "multipart/form-data" You can foloow the examples in this response to try it : Uploading/Displaying Images in MVC 4

Hope it helps

Community
  • 1
  • 1
Dypso
  • 563
  • 1
  • 5
  • 15