Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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# 将用户输入保存到现有文件ASP.NET MVC的操作结果_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 将用户输入保存到现有文件ASP.NET MVC的操作结果

C# 将用户输入保存到现有文件ASP.NET MVC的操作结果,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,有人能解释一下这个代码的错误吗。我需要将categoryInput保存到一个已经存在的txt文件Category.txt中的一行,我试图保存它的方式有问题。我对MVC还很陌生,虽然它看起来很简单,但对于如何实现我的模型以及它为什么存在的目的,我真的很困惑……我理解模型是什么,但在我当前的环境和问题中,我很难理解 型号代码: public class CategoryModel { public int categoryID { get; set; } p

有人能解释一下这个代码的错误吗。我需要将
categoryInput
保存到一个已经存在的txt文件
Category.txt
中的一行,我试图保存它的方式有问题。我对MVC还很陌生,虽然它看起来很简单,但对于如何实现我的模型以及它为什么存在的目的,我真的很困惑……我理解模型是什么,但在我当前的环境和问题中,我很难理解

型号代码:

 public class CategoryModel
    {
        public int categoryID { get; set; }
        public string categoryName { get; set; }
    }
控制器代码:

[HttpPost]
public ActionResult Category(string categoryInput)
{
    var userData = categoryInput + "," + 0 + Environment.NewLine;

     var dataFile = Server.MapPath("~/App_Data/Category.txt");
     File.WriteAllText(@dataFile, userData);
     return View();
}
查看代码:

 @using (Html.BeginForm("Category", "Category", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <form>
            <div class="form-group">
                <label for="categoryInput">Description:</label>
                <input type="text" class="form-control" name="categoryInput">
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
            <button type="button" class="btn btn-secondary" onclick="location.href='@Url.Action("List","List")'">Back</button>
        </form>
    }
@使用(Html.BeginForm(“Category”,“Category”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
说明:
提交
返回
}

错误是什么?它只是不保存?您试过调试吗?错误似乎在
File.WriteAllText(@dataFile,userData)行中它表示Control.file(Byth[],String)是一种在给定上下文中无效的方法。您必须使用<代码> Stry.IO .file < /Cord>。在试图保留该文件中的每个请求的数据列表的情况下,您需要考虑使用<代码> AppEdTrime/Cult>方法。此处有更多详细信息:控制器有一个文件属性,当您在顶部使用System.IO时,该属性与System.IO.File冲突。因此,要告诉它使用System.IO.File,您必须完全限定它,例如:
System.IO.File.writealText
。但是请记住,如果您想保留文件的现有内容并继续添加到其中,WriteAllText可能也不是您想要的。阅读并选择合适的方法。错误是什么?它只是不保存?您试过调试吗?错误似乎在
File.WriteAllText(@dataFile,userData)行中它表示Control.file(Byth[],String)是一种在给定上下文中无效的方法。您必须使用<代码> Stry.IO .file < /Cord>。在试图保留该文件中的每个请求的数据列表的情况下,您需要考虑使用<代码> AppEdTrime/Cult>方法。此处有更多详细信息:控制器有一个文件属性,当您在顶部使用System.IO时,该属性与System.IO.File冲突。因此,要告诉它使用System.IO.File,您必须完全限定它,例如:
System.IO.File.writealText
。但是请记住,如果您想保留文件的现有内容并继续添加到其中,WriteAllText可能也不是您想要的。阅读并选择合适的方法。