Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC中从文本文件中获取数据_C#_Model View Controller - Fatal编程技术网

C# 在MVC中从文本文件中获取数据

C# 在MVC中从文本文件中获取数据,c#,model-view-controller,C#,Model View Controller,我想将特定文件夹中的文本文件中的数据提取到div标记中。。 MVC对我来说是个新概念,请简单解释一下 控制器 public ActionResult filexist() { string subPath = "~/Content/74/74/6_Hall0/Text/data.txt"; bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));

我想将特定文件夹中的文本文件中的数据提取到div标记中。。 MVC对我来说是个新概念,请简单解释一下

控制器

 public ActionResult filexist()
        { 
            string subPath = "~/Content/74/74/6_Hall0/Text/data.txt";
            bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));

            if (!exists)
                System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
            else
            {

            }
            return View();
        }
HTML:

首先,, 控制器中的默认方法是GET, 因此,如果你想使用Post,你必须这样做:

在控制器中添加[HttpPost]:

[HttpPost] 公共行动结果文件存在 { 字符串子路径=~/Content/74/74/6_Hall0/Text/data.txt; bool exists=System.IO.Directory.ExistsServer.mappath子路径; 如果!存在 System.IO.Directory.CreateDirectoryServer.MapPathsubPath; 其他的 { } 返回视图;
}您可以将其用于模型,也可以不用于模型,如果您愿意,我将发布一个带有model…public class Shared{public String getFileData的示例{String filename=~/Content/74/74/6_Hall0/Text/data.txt;String Textline=;System.IO.StreamReader sr=new System.IO.StreamReaderfilename;do{Textline+=sr.ReadLine;}而sr.Peek!=-1;返回Textline;}获取数据正确吗?在控制器公共操作结果文件中{String data=;String subPath=~/Content/74/74/6_Hall0/Text/data.txt;bool exists=System.IO.Directory.ExistsServer.mappath subPath;if!exists System.IO.Directory.CreateDirectoryServer.mappath subPath;else{Shared s=new Shared;data=s.getFileData;}return View;}不知道如何将数据从控制器显示到该文本区域请稍等,我会编辑我的帖子。是否将文件内容发送到视图?
   <form action="" method="post">
    <div>
        <textarea rows="3" cols="15" contenteditable="true" name="data"></textarea>
        <input type="submit" value="Submit" />
    </div>
    </form>