TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sally Pepin
NA
34
3.2k
Display ITEMS from folder - ASP.Net MVC
Aug 4 2017 9:48 AM
Afternoon,
I wish to know if there's a way I can display in a view the items(files) from a folder inside the application.
-------- or also, need help to
How to Download ITEM(file or image) from DB
Hello, I need help with the code to download the item or object I keep on my db.
This is my DocumentController and its actions. You can find list for showing, submit, and also details, delete.
using
PMPDI_APP.Models;
using
System;
using
System.Collections.Generic;
using
System.Data.Entity;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
PagedList;
using
PagedList.Mvc;
using
System.Web.Hosting;
using
System.IO;
using
System.Net;
using
System.Data.SqlClient;
namespace
PMPDI_APP.Controllers
{
public
class
DocumentController : Controller
{
PMPDIEntities db =
new
PMPDIEntities();
// CreateList
public
ActionResult List(string document)
{
var docum = db.Documents.Include(p => p.Project);
var a = from p in db.Documents.Include(p => p.Project)
select p;
if
(!String.IsNullOrEmpty(document))
{
a = a.Where(
c => c.FileDName.Contains(document)
|| c.Project.nameProject.Contains(document)
|| c.TypeDocument.typeDocumentName.Contains(document)
);
}
return
View(a.ToList());
}
// CreateInsert
public
ActionResult submit()
{
ViewBag.ProjectID =
new
SelectList(db.Projects,
"ProjectID"
,
"nameProject"
);
ViewBag.typeDocumentID =
new
SelectList(db.TypeDocuments,
"typeDocumentID"
,
"typeDocumentName"
);
return
View();
}
[HttpPost, ValidateAntiForgeryToken]
public
ActionResult submit(Document t, HttpPostedFileBase file, [Bind(Include =
"DocumentID, Doc, FileDName, typeDocumentID, ProjectID, FileContent, ContentType"
)]
Document document)
{
if
(file != null && file.ContentLength > 0)
{
string ds = file.FileName.Substring(file.FileName.Length - 3);
string p = string.Empty;
p = Server.MapPath(
"~/Files/"
);
file.SaveAs(p + file.FileName);
if
(file.ContentLength > 0)
{
BinaryReader br =
new
BinaryReader(file.InputStream);
byte[] buffer = br.ReadBytes(file.ContentLength);
using
(db)
{
t.Doc = file.FileName;
t.ContentType = file.ContentType;
t.FileContent = buffer;
db.Documents.Add(t);
db.SaveChanges();
}
}
}
else
{
ViewBag.ProjectID =
new
SelectList(db.Projects,
"ProjectID"
,
"nameProject"
);
ViewBag.typeDocumentID =
new
SelectList(db.TypeDocuments,
"typeDocumentID"
,
"typeDocumentName"
);
return
Content(
"Select File"
);
}
return
RedirectToAction(
"List"
);
}
// Details
public
ActionResult Details(
int
id = 0)
{
ViewBag.ProjectID =
new
SelectList(db.Projects,
"ProjectID"
,
"nameProject"
);
ViewBag.typeDocumentID =
new
SelectList(db.TypeDocuments,
"typeDocumentID"
,
"typeDocumentName"
);
return
View(db.Documents.Find(id));
}
/*// Update
public ActionResult Edit(int id = 0)
{
return View(db.Documents.Find(id));
}
*/
// GET: Projects/Edit/
public
ActionResult Edit(
int
? id)
{
if
(id == null)
{
return
new
HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Document document = db.Documents.Find(id);
if
(document == null)
{
return
HttpNotFound();
}
ViewBag.ProjectID =
new
SelectList(db.Projects,
"ProjectID"
,
"nameProject"
);
ViewBag.typeDocumentID =
new
SelectList(db.TypeDocuments,
"typeDocumentID"
,
"typeDocumentName"
);
return
View(document);
}
// POST: Documents/Edit/
[HttpPost, ValidateAntiForgeryToken]
public
ActionResult Edit(Document t, HttpPostedFileBase file, [Bind(Include =
"DocumentID, Doc, FileDName, typeDocumentID, ProjectID"
)] Document document)
{
if
(file != null)
{
string sd = file.FileName.Substring(file.FileName.Length - 3);
string p = string.Empty;
p = Server.MapPath(
"~/Files/"
);
file.SaveAs(p + file.FileName);
using
(db)
{
t.Doc = file.FileName;
db.Entry(t).State = EntityState.Modified;
db.SaveChanges();
}
}
ViewBag.ProjectID =
new
SelectList(db.Projects,
"ProjectID"
,
"nameProject"
);
ViewBag.typeDocumentID =
new
SelectList(db.TypeDocuments,
"typeDocumentID"
,
"typeDocumentName"
);
return
View(document);
}
public
ActionResult Delete(
int
id = 0)
{
return
View(db.Documents.Find(id));
}
[HttpPost, ActionName(
"Delete"
)]
public
ActionResult delete_conf(
int
id = 0)
{
Document t = db.Documents.Find(id);
db.Documents.Remove(t);
db.SaveChanges();
return
RedirectToAction(
"List"
);
}
}
}
If my question is not complete, please, let me know so I can update it.
Reply
Answers (
1
)
How to restore dabase in c# application send
moving datagridview column to checkedlistbox in another form