简体   繁体   English

我如何 append 将文本输入到 ASP.NET MVC 项目文件中的 .txt 文件中?

[英]How could i append inputted text into a .txt file within an ASP.NET MVC project file?

I am currently busy with a project which is suppose to take a user input from the 'categoryDescription' input text box and append it to an already created text file which i have put under the folder, 'app_data', saved under the name of 'category.txt'.我目前正忙于一个项目,该项目假设从“categoryDescription”输入文本框和 append 获取用户输入到一个已经创建的文本文件,我将其放在文件夹“app_data”下,以“名称”保存类别.txt'。 I am very new to ASP.NET MVC and am rather lost to say the very least.. i am unsure as to where to go from here on-wards..我对 ASP.NET MVC 非常陌生,至少可以说我很迷茫。我不确定从这里开始到 go 的位置。

Below i have attached a code segment which is supposedly responsible for the appending of the input to the text file: (side note: this code falls under the 'Category.cshtml' view, controlled by 'CategoryController.cs')下面我附上了一个代码段,它应该负责将输入附加到文本文件中:(旁注:此代码属于“Category.cshtml”视图,由“CategoryController.cs”控制)

@{
    var result = "";
    if (IsPost)
    {
        var description = Request["categoryDescription"];

        var userData = description + Environment.NewLine;

        var dataFile = Server.MapPath("~/App_Data/Category.txt");
        File.WriteAllText(dataFile, userData);
        result = "Information Saved.";
    }
}

And hereafter is the code for the input and final if statement to check whether the 'IsPost' property has been submitted before i have started processing.以下是输入和最终 if 语句的代码,用于检查在我开始处理之前是否已提交“IsPost”属性。

@using (Html.BeginForm("Index", "Categories"))
{
    <input id="categoryDescription " type="text" name="categoryDescription" />

    <input type="submit" value="submit"/>

}

@if (result != "")
{
    <p>Result: @result</p>
}

I apologize if i have been unable to explain any clearer, i will answer questions as soon as possible.如果我无法更清楚地解释,我深表歉意,我会尽快回答问题。 Thank you:)谢谢:)

Instead of WriteAllText use AppendAllText :而不是WriteAllText使用AppendAllText

File.AppendAllText(dataFile, userData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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