0

i am trying to create a crud application but whenever i am trying to upload in edit mode, it gets returned null. Well i am not uploading the same in first time, but i am uploading this in edit mode only.I am uploading two files one at first tym and the other file will be null at that time, the other file will only be uploaded at edit mode and at that tym the first file will be uneditable

public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            FileDetails fileDetails = db.FileUpload.Find(id);
            if (fileDetails == null)
            {
                return HttpNotFound();
            }
            return View(fileDetails);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit( FileDetails fileDetails)
        {
            if (ModelState.IsValid)
            {
                string uploadedfilename = Path.GetFileName(fileDetails.fileaftertourupload.FileName);
                if (!string.IsNullOrEmpty(uploadedfilename))
                {
                    string filenamewithoutextension = Path.GetFileNameWithoutExtension(fileDetails.fileaftertourupload.FileName);
                    string extension = Path.GetExtension(fileDetails.fileaftertourupload.FileName);
                    string filename = filenamewithoutextension + DateTime.Now.ToString("yymmssfff") + extension;
                    fileDetails.FileAfterTourName = filename;
                    fileDetails.FileAfterTour = "~/Content/Files/" + filename;
                    filename = Path.Combine(Server.MapPath("~/Content/Files"), filename);
                    fileDetails.fileaftertourupload.SaveAs(filename);

                    db.Entry(fileDetails).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            return View(fileDetails);
        }
@model OnlineStationaryRegister.Models.FileDetails

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


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

    <div class="form-horizontal">
        <h4>FileDetails</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.FileId)

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.FileBeforeTour, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <a href="~/Content/Files/@Model.FileBeforeTourName" target="_blank">View File</a>

            </div>
        </div>

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



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

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace OnlineStationaryRegister.Models
{
    public class FileDetails
    {
        [Key]
        public int FileId { get; set; }
        public string Officername { get; set; }
        public string Designation { get; set; }
        public string FileBeforeTour { get; set; }
        public string FileAfterTour { get; set; }
        public string FileBeforeTourName { get; set; }
        public string FileAfterTourName { get; set; }
        public int  status { get; set; } = 1;
        [NotMapped]
        public HttpPostedFileBase filebeforetourupload { get; set; }
        [NotMapped]
        public HttpPostedFileBase fileaftertourupload { get; set; }
    }
}

1 Answers1

1

First for upload image you should add enctype attribute in beginform.

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

then next if send multi image through multipart ,you should get bytes by HttpContext.Current.Request.Files in action.

Sample Code Upload Image by using asp.net mvc

How to Upload Image Via WebApi

Amin Golmahalleh
  • 3,585
  • 2
  • 23
  • 36
  • thanks it worked, but due to this i am going through one more issue, the previous uploaded file gets blank , why? – sunny kumar Jan 13 '20 at 09:23
  • It is not necessary you add two properties with names filebeforetourupload and fileaftertourupload in model. you can get images by HttpContext As has been said above. OR you can use IEnumerable in argument action for get images.please see this link: https://stackoverflow.com/questions/35017713/upload-multiple-files-filepath-using-asp-net-mvc – Amin Golmahalleh Jan 13 '20 at 10:22
  • i understand you bro, but why is this issue coming can u tell me, that when i go to edit and click on button to update why does it makes the pre uploaded one blank – sunny kumar Jan 13 '20 at 13:02