简体   繁体   中英

fetch data from text file in mvc4 c#

I want to fetch data from text file that is in particular folder into div tag.. MVC is new for me..So explain me briefly..

controller

 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>

first of all, the default Method in the controller is "GET", so if you want to use Post so you have to do like this:

in your Controller add "[HttpPost]":

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

and then, in your HTML(View):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM