Problem adding the Image Directory to the database using ASP.NET

Asked

Viewed 18 times

1

Controller

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Uploadimagemvc.Models; using System.IO;

namespace Uploadimagemvc.Controllers { public class Imagecontroller : Controller { // GET: Image [Httpget] public Actionresult Add() { Return View(); }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Add(Image image)
    {
        string filename = Path.GetFileNameWithoutExtension(image.ImageFile.FileName); 
        string extension = Path.GetExtension(image.ImageFile.FileName);
        filename = filename + DateTime.Now.ToString("yymmssfff") + extension;
        image.ImagePath = "~/Image/" + filename;
        filename = Path.Combine(Server.MapPath("~/Image/"), filename);
        image.ImageFile.SaveAs(filename);
        using (DBModels db = new DBModels())
        {

            db.Images.Add(image);
            db.SaveChanges();
        }
        ModelState.Clear();
            return View();
    }


    [HttpGet]
    public ActionResult View (int id)
    {
        Image image = new Image();
        using (DBModels db = new DBModels())
        {
            image = db.Images.Where(x => x.ImageID == id).FirstOrDefault();
        }

        return View(image);


    }
}

}

View

@model Uploadimagemvc.Models.Image

@{ Viewbag.Title = "Add"; }

Add

@using (Html.Beginform("Add","Image", Formmethod.Post, new { enctype="Multipart/form-data " }) { @Html.Antiforgerytoken()

Image
@Html.Validationsummary(true, "", new { @class = "text-Danger" }) @Html.Labelfor(model => model.Title, htmlAttributes: new { @class = "control-label col-Md-2" }) @Html.Editorfor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.Validationmessagefor(model => model.Title, "", new { @class = "text-Danger" }) @Html.Labelfor(model => model.Imagepath, htmlAttributes: new { @class = "control-label col-Md-2" }) @Html.Editorfor(model => model.Imagepath, new { htmlAttributes = new { @class = "form-control", type = "file" } }) @Html.Validationmessagefor(model => model.Imagepath, "", new { @class = "text-Danger" })
}

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

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

Model

//------------------------------------------------------------------------------

// // This code was generated from a template. // // Manual changes to this file may cause Unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // //------------------------------------------------------------------------------

namespace Uploadimagemvc.Models { using System; using System.Collections.Generic; using System.Componentmodel; using System.Web;

public partial class Image
{
    public int ImageID { get; set; }
    public string Title { get; set; }

    [DisplayName("UploadFile")]
    public string ImagePath { get; set; }

    public HttpPostedFileBase ImageFile { get; set; }
}

}

enter the code here

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.