I have an Assignment model which stores a StudentName and a .zip file (amongst other items) to a database. I have tested out my Create method, and it manages to create the Assignment with the corresponding File selected and uploads it to the database for storage. That portion is working correctly. I am having trouble displaying the stored file in my View as a downloadable link.
Here is how the Database is set up.
Assignments Table:
Files Table:
The File in the Files table associates a relationship with the Assignment through a foreign key relationship where AssignmentId is stored in each File entry in the database.
When I run it and create an Assignment and look at the table values in the Database, I can see that the file is being uploaded and stored and the relationships created. Here is how my models look.
Assignment Model:
namespace ProjectVoting.Models
{
public class Assignment
{
[Key]
public int Id { get; set; }
public string StudentName { get; set; }
public int Votes { get; set; }
public virtual ICollection<File> Files { get; set; }
}
}
File Model:
namespace ProjectVoting.Models
{
public class File
{
public int FileId { get; set; }
[StringLength(255)]
public string FileName { get; set; }
[StringLength(100)]
public string ContentType { get; set; }
public byte[] Content { get; set; }
public FileType FileType { get; set; }
public int AssignmentId { get; set; }
public virtual Assignment Assignment { get; set; }
}
}
FileType Class:
namespace ProjectVoting.Models
{
public enum FileType
{
Project = 1, ZipFile
}
}
In the Index view of the Assignment model, I am trying to display a list of the StudentName, File, and the Edit/Details/Delete links. Here is how I have the view set up.
Assignment/Index View:
@model IEnumerable<ProjectVoting.Models.Assignment>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.StudentName)
</th>
<th>
@Html.DisplayNameFor(model => model.Votes)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.StudentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Votes)
</td>
<td>
@Html.DisplayFor(modelItem => item.Files)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
Note, I am using @Html.DisplayFor(modelItem => item.Files) to try to display a download link to the file, from the database. However, when I get to the index page, it is shown as this:
What is the proper way to display the downloadable link to the File from the database in the table?
You need to create additional action method inside your controller for downloading the file.
In you case, in your View you need to replace
@Html.DisplayFor(modelItem => item.Files)
With loop with downloading link for each file
@foreach(var file in item.Files)
{
@Html.ActionLink("Download "+file.FileName, "Download", new { id=file.FileId })
}
And in your controller add new action
public ActionResult Download(int id)
{
//get File by fileId
var file = ........
byte[] contents = file.Content;
return File(contents, file.ContentType)
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments