Search code examples
asp.net-mvc-5

the view "Delete" or its master was not found asp.net mvc5


I want to be able to upload a file then to download it or delete it. But when I try to delete it, I get this error:

The view 'Delete' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/FileUpload/Delete.aspx ,~/Views/FileUpload/Delete.ascx, ~/Views/Shared/Delete.aspx,~/Views/Shared/Delete.ascx, ~/Views/FileUpload/Delete.cshtml, ~/Views/FileUpload/Delete.vbhtml, ~/Views/Shared/Delete.cshtml ,~/Views/Shared/Delete.vbhtml .

  [HttpGet]
    public ActionResult Delete( string deletedfile)
    {
         string current_usr = User.Identity.GetUserId();


        string fullPath = Request.MapPath("~/Files/" + current_usr + "/" + deletedfile);

        if (System.IO.File.Exists(fullPath))
        {
            System.IO.File.Delete(fullPath);
            ViewBag.Message="Deleted";
        }
        var items = GetFiles();

        return View(items);

    }
  // GET: FileUpload
        public ActionResult Index()
        {
            var items = GetFiles();
            return View(items);
        }

        // POST: FileUpload
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {

            if (file != null && file.ContentLength > 0)
                try
                {

                    string current_usr = User.Identity.GetUserId();


                    //string path = Path.Combine(Server.MapPath("~/Files/"),
                    //    Path.GetFileName(file.FileName));

                    var folder = Server.MapPath("~/Files/" + current_usr + "/");
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }

                    string path = Path.Combine(String.Format(folder),
                       Path.GetFileName(file.FileName));

                    file.SaveAs(path);
                    ViewBag.Message = "File uploaded successfully";

                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }

            var items = GetFiles();

            return View(items);

        }


        public FileResult Download(string downloadedfile)
        {
            string current_usr = User.Identity.GetUserId();

            var FileVirtualPath = "~/Files/" + current_usr + "/" + downloadedfile;

            return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));

        }



        private List<string> GetFiles()
        {

            string current_usr = User.Identity.GetUserId();


            var dir = new System.IO.DirectoryInfo(Server.MapPath("~/Files/" + current_usr + "/"));
            System.IO.FileInfo[] fileNames = dir.GetFiles("*.*");

            List<string> items = new List<string>();

            foreach (var file in fileNames)
            {
                items.Add(file.Name);
            }

            return items;

        }

The View :

<h2> File Upload </h2>

@model List<string>

@using (Html.BeginForm("Index", "FileUpload", FormMethod.Post,
        new { enctype = "multipart/form-data" }))
{


    <label for="file"> Upload </label>
    <input type="file" name="file" id="file" />
    <br /><br />

    <input type="submit" value="Upload" />
    <br /><br />

    @ViewBag.Message

    <br />

    <h2>Documents list</h2>

    <table style="width:100%">
        <tr>
            <th> File Name </th>
            <th> Link  </th>
        </tr>

        @for (var i = 0; i <= (Model.Count) - 1; i++)
        {
        <tr>

            <td>@Model[i].ToString() </td>

            <td>@Html.ActionLink("Download", "Download", new { downloadedfile = Model[i].ToString() }) </td>

            <td>
               
                @Html.ActionLink("Delete", "Delete", new { deletedfile = Model[i].ToString() }) 
            </td>

            

        </tr>

        }


    </table>
}

Solution

  • The issue is that your Delete Controller method is calling View() at the end. That method will attempt to find a view file with the name of the controller method. If you want to show the list of files after the delete you can redirect to your index action like this:

        [HttpGet]
        public ActionResult Delete(string deletedfile)
        {
            string current_usr = User.Identity.GetUserId();
            string fullPath = Request.MapPath("~/Files/" + current_usr + "/" + deletedfile);
    
            if (System.IO.File.Exists(fullPath))
            {
                System.IO.File.Delete(fullPath);
                ViewBag.Message = "Deleted";
            }
    
            return RedirectToAction("Index");
        }
    

    See this link from the microsoft Docs for more detail on redirecting

    https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/asp-net-mvc-views-overview-cs