从mvc4 c中的文本文件中获取数据#

时间:2015-02-18 06:50:07

标签: 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));

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

            }
            return View();
        }

HTML:

   <form action="" method="post">
    <div>
        <textarea rows="3" cols="15" contenteditable="true" name="data"></textarea>
        <input type="submit" value="Submit" />
    </div>
    </form>

1 个答案:

答案 0 :(得分:0)

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

在你的Controller中添加&#34; [HttpPost]&#34;:

&#13;
&#13;
[HttpPost]
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();
}
&#13;
&#13;
&#13;

然后,在HTML(视图)中:

&#13;
&#13;
@using (Html.BeginForm("filexist", "Your_Controller_Name", FormMethod.Post))
{ 
  <div>
    <textarea rows="3" cols="15" contenteditable="true" name="data"></textarea>
    <input type="submit" value="Submit" />
  </div>
}
    
&#13;
&#13;
&#13;