Hi sunnyk21,
Check this example. Now please take its reference and correct your code.
Model
public class TourDetailModels
{
[Key]
public int id { get; set; }
[Required]
[Display(Name = "Subject")]
public string Subject { get; set; }
[Required]
[Display(Name = "Tour Date")]
public string TourYear { get; set; }
[Required]
[Display(Name = "Total Foreign Tours")]
public int? ForeignTourCount { get; set; }
[Required]
[Display(Name = "Country")]
public string Country { get; set; }
[Required]
[Display(Name = "Officer Name")]
public string OfficerName { get; set; }
[Display(Name = "Status")]
public string status { get; set; }
public DateTime? timestamp { get; set; }
[Display(Name = "Data Filled By")]
public string DataFilledBy { get; set; }
[Display(Name = "PPT Of Before Tour")]
public string PptBeforeTour { get; set; }
public string filename { get; set; }
public HttpPostedFileBase pptbeforetourpath { get; set; }
public HttpPostedFileBase pptaftertourpath { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: /Home/
private TestEntities db = new TestEntities();
public ActionResult Index()
{
return View(GetTourDetails());
}
public ActionResult Details(int? id)
{
if (id == null)
{
return new EmptyResult();
}
TourDetailModels tourDetailModels = db.TourDetails.Where(x => x.id == id)
.Select(x => new TourDetailModels
{
id = x.id,
Subject = x.Subject,
TourYear = x.TourYear,
ForeignTourCount = x.ForeignTourCount,
Country = x.Country,
OfficerName = x.OfficerName,
status = x.status,
timestamp = x.timestamp,
DataFilledBy = x.DataFilledBy,
filename = x.filename
}).FirstOrDefault();
if (tourDetailModels == null)
{
return new EmptyResult();
}
return View(tourDetailModels);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(TourDetailModels td)
{
TourDetail details = new TourDetail();
if (ModelState.IsValid)
{
string uploadedFileName = Path.GetFileName(td.pptbeforetourpath.FileName);
if (!string.IsNullOrEmpty(uploadedFileName))
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(td.pptbeforetourpath.FileName);
string extension = Path.GetExtension(td.pptbeforetourpath.FileName);
string filename = fileNameWithoutExtension + DateTime.Now.ToString("yymmssfff") + extension;
details.PptBeforeTour = "~/Content/Files/" + filename;
filename = Path.Combine(Server.MapPath("~/Content/Files"), filename);
td.pptbeforetourpath.SaveAs(filename);
details.PptBeforeTour = details.PptBeforeTour;
}
// Update columns except PptBeforeTour and filename column.
details.Subject = td.Subject;
details.TourYear = td.TourYear;
details.ForeignTourCount = td.ForeignTourCount;
details.Country = td.Country;
details.OfficerName = td.OfficerName;
details.status = td.status;
details.timestamp = td.timestamp;
details.DataFilledBy = td.DataFilledBy;
// Save the database changes.
db.TourDetails.AddObject(details);
db.SaveChanges();
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
public ActionResult Download(int id)
{
TourDetail file = db.TourDetails.Where(x => x.id == id).FirstOrDefault();
if (System.IO.File.Exists(Server.MapPath(file.PptBeforeTour)))
{
byte[] bytes = System.IO.File.ReadAllBytes(Server.MapPath(file.PptBeforeTour));
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, file.filename);
}
return RedirectToAction("Index");
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new EmptyResult();
}
TourDetailModels tourDetailModels = db.TourDetails.Where(x => x.id == id)
.Select(x => new TourDetailModels
{
id = x.id,
Subject = x.Subject,
TourYear = x.TourYear,
ForeignTourCount = x.ForeignTourCount,
Country = x.Country,
OfficerName = x.OfficerName,
status = x.status,
timestamp = x.timestamp,
DataFilledBy = x.DataFilledBy,
filename = x.filename
}).FirstOrDefault();
if (tourDetailModels == null)
{
return new EmptyResult();
}
return View(tourDetailModels);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(TourDetailModels tourDetailModels)
{
if (ModelState.IsValid)
{
TourDetail databaseFileName = db.TourDetails.Where(x => x.id == tourDetailModels.id).FirstOrDefault();
// Check if file is uploaded then update PptBeforeTour and filename column.
if (tourDetailModels.pptaftertourpath != null)
{
string uploadedFileName = Path.GetFileName(tourDetailModels.pptaftertourpath.FileName);
if (!string.IsNullOrEmpty(uploadedFileName))
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(tourDetailModels.pptaftertourpath.FileName);
string extension = Path.GetExtension(tourDetailModels.pptaftertourpath.FileName);
string filename = fileNameWithoutExtension + DateTime.Now.ToString("yymmssfff") + extension;
databaseFileName.filename = filename;
tourDetailModels.PptBeforeTour = "~/Content/Files/" + filename;
filename = Path.Combine(Server.MapPath("~/Content/Files"), filename);
tourDetailModels.pptaftertourpath.SaveAs(filename);
databaseFileName.PptBeforeTour = tourDetailModels.PptBeforeTour;
}
}
// Update columns except PptBeforeTour and filename column.
databaseFileName.Subject = tourDetailModels.Subject;
databaseFileName.TourYear = tourDetailModels.TourYear;
databaseFileName.ForeignTourCount = tourDetailModels.ForeignTourCount;
databaseFileName.Country = tourDetailModels.Country;
databaseFileName.OfficerName = tourDetailModels.OfficerName;
databaseFileName.status = tourDetailModels.status;
databaseFileName.timestamp = tourDetailModels.timestamp;
databaseFileName.DataFilledBy = tourDetailModels.DataFilledBy;
// Save the database changes.
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tourDetailModels);
}
// GET: Tour/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new EmptyResult();
}
TourDetailModels tourDetailModels = db.TourDetails.Where(x => x.id == id)
.Select(x => new TourDetailModels
{
id = x.id,
Subject = x.Subject,
TourYear = x.TourYear,
ForeignTourCount = x.ForeignTourCount,
Country = x.Country,
OfficerName = x.OfficerName,
status = x.status,
timestamp = x.timestamp,
DataFilledBy = x.DataFilledBy,
filename = x.filename
}).FirstOrDefault();
if (tourDetailModels == null)
{
return new EmptyResult();
}
return View(tourDetailModels);
}
// POST: Tour/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
TourDetail DetailModel = db.TourDetails.Where(x => x.id == id).FirstOrDefault();
db.TourDetails.DeleteObject(DetailModel);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private List<TourDetailModels> GetTourDetails()
{
List<TourDetailModels> model = db.TourDetails
.Select(x => new TourDetailModels
{
id = x.id,
Subject = x.Subject,
TourYear = x.TourYear,
ForeignTourCount = x.ForeignTourCount,
Country = x.Country,
OfficerName = x.OfficerName,
status = x.status,
timestamp = x.timestamp,
DataFilledBy = x.DataFilledBy,
filename = x.filename
}).ToList();
return model;
}
}
View
Index
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Edit_Update_FileUpload_MVC.Models.TourDetailModels>>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>Subject</th>
<th>TourYear</th>
<th>ForeignTourCount</th>
<th>Country</th>
<th>OfficerName</th>
<th>status</th>
<th>timestamp</th>
<th>DataFilledBy</th>
<th>PptBeforeTour</th>
<th>filename</th>
<th></th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td><%: item.id %></td>
<td><%: item.Subject %></td>
<td><%: item.TourYear %></td>
<td><%: item.ForeignTourCount %></td>
<td><%: item.Country %></td>
<td><%: item.OfficerName %></td>
<td><%: item.status %></td>
<td><%: String.Format("{0:g}", item.timestamp) %></td>
<td><%: item.DataFilledBy %></td>
<td><%: item.PptBeforeTour %></td>
<td><%: item.filename %></td>
<th><%: Html.ActionLink("Edit","Edit", new { id=item.id }) %></th>
</tr>
<% } %>
</table>
<p><%: Html.ActionLink("Create New", "Create") %></p>
</body>
</html>
Edit
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Edit_Update_FileUpload_MVC.Models.TourDetailModels>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Edit</title>
</head>
<body>
<% using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<%: Html.ValidationSummary(true) %>
<%: Html.AntiForgeryToken() %>
<fieldset>
<legend>Update Tour Details</legend>
<div class="editor-label">
<%: Html.HiddenFor(model => model.id)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Subject) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Subject)%>
<%: Html.ValidationMessageFor(model => model.Subject) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.TourYear) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.TourYear)%>
<%: Html.ValidationMessageFor(model => model.TourYear) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ForeignTourCount) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.ForeignTourCount)%>
<%: Html.ValidationMessageFor(model => model.ForeignTourCount) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Country) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Country)%>
<%: Html.ValidationMessageFor(model => model.Country) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.OfficerName) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.OfficerName)%>
<%: Html.ValidationMessageFor(model => model.OfficerName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.status) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.status)%>
<%: Html.ValidationMessageFor(model => model.status) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.timestamp) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.timestamp, String.Format("{0:g}", Model.timestamp))%>
<%: Html.ValidationMessageFor(model => model.timestamp) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DataFilledBy) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.DataFilledBy)%>
<%: Html.ValidationMessageFor(model => model.DataFilledBy) %>
</div>
<div class="editor-label">
<%: Html.Label("View PPT of Before Tour") %>
</div>
<div class="editor-field">
<%--<%: Html.TextBoxFor(model => model.PptBeforeTour) %>--%>
<a class="title" href="/Content/Files/<%=Model.filename %>" target="_blank" style="margin-top: 10px">
View PPT</a>
</div>
<div class="editor-label">
<%: Html.Label("Ppt of After Tour") %>
</div>
<div class="editor-field">
<input type="file" name="pptaftertourpath" class="form-control" />
</div>
<p>
<input type="submit" value="Update Tour Details" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</body>
</html>