如何在没有操作方法的情况下重定向到视图

时间:2015-06-17 14:54:56

标签: c# asp.net asp.net-mvc-4

我想重定向到一个视图,但没有动作方法。因为它不是必需的。

为什么我要这样做。因为您可以删除文件(DeleteFile),也可以删除文件夹(DeleteFolder)。

我有这个:

public ActionResult DeleteFile(string designId, int fileId)
        {
            DeleteFileModel DeleteFile = new DeleteFileModel();
            TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
            FileTree tree = CreateTree(designId, entry.FilePath);
            FileInfo fileInfo = new FileInfo(tree.Files[fileId].FullPath);
            DirectoryInfo directoryInfo = new DirectoryInfo(tree.Files[fileId].FullPath);

            if (directoryInfo.Attributes == FileAttributes.Directory) {
                //Url.Action(designId, "DeleteFolder");


                string directoryName = tree.Files[fileId].FileName;
                DeleteFile.DesignId = designId;
                DeleteFile.FileName = directoryName;
                DeleteFile.Id = fileId;
                DeleteFile.ModificationDate = fileInfo.LastWriteTime;

                DeleteFile.RelativePath = fileInfo.Directory.Name + "/" + directoryName;
                AddFileExplorerBreadCrumb(designId);
            }
            else {

                string fileName = tree.Files[fileId].FileName;
                DeleteFile.DesignId = designId;
                DeleteFile.FileName = fileName;
                DeleteFile.Id = fileId;
                DeleteFile.ModificationDate = fileInfo.LastWriteTime;
                DeleteFile.FileSize = fileInfo.Length;
                DeleteFile.RelativePath = fileInfo.Directory.Name + "/" + fileName;

                AddFileExplorerBreadCrumb(designId);
            }

            return View(DeleteFile);
        }

因此,如果它是用户想要删除的目录,那么它会查看:DeleteFolder,如果用户想要删除文件,则转到:DeleteFile。但是怎么做呢?

谢谢

2 个答案:

答案 0 :(得分:2)

您需要将return语句放在if和else部分

If( your directory delete condition){
Your directory delete logic
return view("deletedirectory.cshtml");
}
else{
//File delete logic
return view("deletefile.cshtml");
}

答案 1 :(得分:0)

如果我正确理解了您的问题,您可以在删除'之后尝试显示某种确认页面。调用操作,并且您希望指定哪个页面(文件与目录),而不必为每个页面创建操作。

您可以通过在View()方法中指定所需视图的名称来执行此操作。

return View("DeleteFileConfirmation.cshtml", model)

return View("DeleteFolderConfirmation.cshtml", model)