View Model :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication7.Models
{
public class EMPVM
{
public int EID { get; set; }
public string NAME { get; set; }
public List<HttpPostedFileBase> UPLOAD { get; set; }
public List<EMPMAP> EMPMAPS { get; set; }
}
}
Create View :
@model WebApplication7.Models.EMPVM
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm("Create", "Test", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.EID, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EID)
@Html.ValidationMessageFor(model => model.EID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NAME, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NAME)
@Html.ValidationMessageFor(model => model.NAME)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UPLOAD, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.UPLOAD, new { @type = "file", @class = "form-control", @multiple = "multiple" })
@Html.ValidationMessageFor(model => model.UPLOAD)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Index View :
@model IEnumerable<WebApplication7.Models.EMPVM>
@{
ViewBag.Title = "Index";
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table table-bordered table-condensed table-hover table-responsive table-striped ">
<tr>
<th>
@Html.DisplayNameFor(model => model.EID)
</th>
<th>
@Html.DisplayNameFor(model => model.NAME)
</th>
<th>ATTACHMENT</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EID)
</td>
<td>
@Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
<table>
@foreach (var items in item.EMPMAPS)
{
<tr>
<td>
@Html.ActionLink(items.FILENAME, "Down", new { ID = items.ID }, new { @class = "btn btn-primary",@style="width:100px" })
</td>
</tr>
}
</table>
</td>
</tr>
}
</table>
Conroller :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication7.Models;
namespace WebApplication7.Controllers
{
public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
using (Database1Entities obj = new Database1Entities())
{
List<EMPVM> lst = obj.EMPs.Select(e => new EMPVM { EID = e.EID, NAME = e.NAME, EMPMAPS=obj.EMPMAPs.Where(m=>m.EID==e.EID).ToList() }).ToList();
return View(lst);
}
}
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(EMPVM vm)
{
try
{
using (Database1Entities obj = new Database1Entities())
{
EMP emp = new EMP();
emp.EID = vm.EID;
emp.NAME = vm.NAME;
obj.EMPs.Add(emp);
obj.SaveChanges();
foreach (HttpPostedFileBase hpfb in vm.UPLOAD)
{
EMPMAP eMPMAP = new Models.EMPMAP();
hpfb.SaveAs(Server.MapPath("~/UPLOADED/" + hpfb.FileName));
eMPMAP.EID = vm.EID;
eMPMAP.FILENAME = hpfb.FileName;
obj.EMPMAPs.Add(eMPMAP);
}
obj.SaveChanges();
return RedirectToAction("Index");
}
}
catch (Exception ex)
{
throw ex;
}
}
[HttpGet]
public ActionResult Down(int ID)
{
try
{
using (Database1Entities obj = new Database1Entities())
{
string Filename = obj.EMPMAPs.SingleOrDefault(m => m.ID == ID).FILENAME;
string path = AppDomain.CurrentDomain.BaseDirectory + "UPLOADED\\";
byte[] fileBytes = System.IO.File.ReadAllBytes(path + Filename);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Filename);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}