【问题标题】:the view "Delete" or its master was not found asp.net mvc5没有找到视图“删除”或其主人 asp.net mvc5
【发布时间】:2020-06-23 16:12:27
【问题描述】:

我希望能够上传文件然后下载或删除它。但是当我尝试删除它时,我得到了这个错误:

未找到“删除”视图或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置:

~/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;

        }

观点:

<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>
}

【问题讨论】:

    标签: asp.net-mvc-5


    【解决方案1】:

    问题是您的 Delete Controller 方法最后调用了 View()。该方法将尝试查找具有控制器方法名称的视图文件。如果您想在删除后显示文件列表,您可以像这样重定向到您的索引操作:

        [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");
        }
    

    有关重定向的更多详细信息,请参阅 Microsoft Docs 中的此链接

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 2012-04-29
      • 2011-01-17
      • 1970-01-01
      • 2015-03-09
      相关资源
      最近更新 更多