C# 在MVC中从文本文件中获取数据
我想将特定文件夹中的文本文件中的数据提取到div标记中。。 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));
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>