-
Notifications
You must be signed in to change notification settings - Fork 0
16 Link to Uploaded Files
M. Fares edited this page Mar 15, 2018
·
3 revisions
This procedure shows how to link to an uploaded file in a view.
-
The files should be saved on a folder in your application (see Upload files procedure) and the paths to these file should be stored in the database.
-
Create a property in your view to hold the file path.
Example: /ViewModels/CourseViewModel.cs
public class CourseViewModel
{
...
// The outline file path as string (used to diplay the path)
public string OutlineFilePath { get; set; }
}- Create the action that gets the data.
Example: Details action in /Controllers/CourseController.cs
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Course course = db.Courses.Find(id);
if (course == null)
{
return HttpNotFound();
}
var model = new CourseViewModel
{
Id = course.CourseId,
Code = course.Code,
Title = course.Title,
Description = course.Description,
OutlineFilePath = course.OutlineFilePath
};
return View(model);
}- Create the view that displays the link.
Example: /Views/Course/Details.cshtml
See the last dd tag and the usage of @Url.Content()
<dd>
@Html.DisplayFor(model => model.Outline)
<a href="@Url.Content(Model.OutlineFilePath)">Course Outline</a>
</dd>