Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Ground Controller Lvl 1
Message 1 of 3

error while uploading image in database using asp.net mvc5 c#

I have a problem, I created a project to upload and display image but when it works and i choose image, it stop debugging and didn't save anything in database and i tried several times to solve it but nothing changes .. here's my controller

public class ImageController : Controller { DbModels db = new DbModels();

// GET: Image
    public ActionResult Index()
    {

        return View();
    }
    [HttpGet]
    public ActionResult Add()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Add(UploadingImage imageModel, HttpPostedFileBase fileImage)
    {
        UploadingImage image = new UploadingImage();
        string path = uploadImage(fileImage);
        if (path.Equals("-1"))
        {

        }
        else
        {
            image.Title = imageModel.Title;
            image.ImagePath = imageModel.ImagePath;
            ViewBag.msg = "Data Added..";
        }

        return View();
    }

    public string uploadImage(HttpPostedFileBase imageFile)
    {
        Random r = new Random();

        string path = "-1";

        int random = r.Next();

        if (imageFile != null && imageFile.ContentLength > 0)
        {
            string extension = Path.GetExtension(imageFile.FileName);
            if (extension.ToLower().Equals(".png"))
            {
                try
                {
                    path = Path.Combine(Server.MapPath("~/Image"), random.ToString(), Path.GetFileName(imageFile.FileName));

                }
                catch (Exception)
                {
                    path = "-1";
                }
            }
        }


        return path;
    }
}

and here's the view

@using System.Web.UI.WebControls
@model UploadingImages.Models.UploadingImage

@{
    ViewBag.Title = "Add";
}

<h1>
    @ViewBag.msg
</h1>

@using (Html.BeginForm("Add", "Image", FormMethod.Post, new {enctype = "multipart/form-data"})) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>UploadingImage</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default"/>
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

if anyone could help me please I am tired of this project but i want to finish it please .... thank youu everyoneee <3

2 Replies
Highlighted
Moderator
Message 2 of 3

Re: error while uploading image in database using asp.net mvc5 c#

Hello @LujainMohammad96

This post was moved to a different board that fits your topic of discussion a bit better. This means you’ll get better engagement on your post, and it keeps our Community organized so users can more easily find information.

As you’ll notice, your Topic is now in the 'Project Development Help & Advice' board. No action is needed on your part; you can continue the conversation as normal here.

Let me know if you have any other questions or if I can help with anything else.

Cheers,

-MChevy422

GitHub Moderator

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Highlighted
Commander Lvl 2
Message 3 of 3

Re: error while uploading image in database using asp.net mvc5 c#

I'm going to have to guess a little here, as you don't provide all the info necessary to assist you. I.e. you don't show your database model declarations. You also don't tell us where it stops - "it stop debugging" is not informative.

 

You don't do anything with your db object, so nothing is put in the database.

 

Assuming that UploadingImage is your database model object, you need to :

1. Write your info into the object.

2. Add the new object instance into the database table. (e.g. db.Images.Add(imageModel))

3. Explicitly save changes to the database (e.g. db.SaveChanges())

 

You might try this video tutorial.

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!