Reputation: 75
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; }
}
}
Upvotes: 0
Views: 53
Reputation: 4206
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
Upvotes: 1