sunny kumar
sunny kumar

Reputation: 75

Not able to retain original values after edit in mvc

i m not able to solve one issue that is , there are two files one is uploaded at the form filled up for the first time and the other file will be uploaded when the form willl be edited , but the issue is that, when the first uploaded file is shown in edit and no changes be made, that files gets blank , i used exclude as well but that does not made any effect.

My Controller methods:

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([Bind(Exclude= "FileBeforeTour,FileBeforeTourName")] 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);
    }

My Edit View:

@model OnlineStationaryRegister.Models.FileDetails

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm("Edit", "File", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
                @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")
}

My FileDetails model:

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; }
    }
}

Upvotes: 1

Views: 243

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32059

It can be solved in many ways. One of the simplest ways as follows:

 db.Entry(fileDetails).State = EntityState.Modified;
 db.Entry(fileDetails).Property(x => x.FileBeforeTourName).IsModified = false; //<-- Here it is
 db.Entry(fileDetails).Property(x => x.FileBeforeTour).IsModified = false; //<-- Here it is
 db.SaveChanges();

Upvotes: 1

Related Questions