c# - 你调用的对象是空的? (异常错误~ASP.NET MVC)

标签 c# asp.net asp.net-mvc model-view-controller

目前,我在 View 中使用已在模型中声明的变量,并在相应 Controller 中分配了值。我使用 Model.variableName 来引用这些变量,但是在调试过程中抛出以下异常;

enter image description here

我在我的 View 中使用以下命名空间;

@using ProjectName.Models
@model Category

我的类(class)模型;

public class Category
    {
        public string result { get; set; }
    }

我的 Controller ;

 public ActionResult Index()
        {
            return View();
        }


[HttpGet]
        public ActionResult ReadCategory()
        {
            var dataFile = Server.MapPath("~/App_Data/Category.txt");

            Category passCategory = new Category
            {
                result = "",
                delimiterChar = new[] { ',' },
                userData = System.IO.File.ReadAllLines(dataFile)
            };

            return View(passCategory);
        }

任何帮助将不胜感激!

最佳答案

尝试将您的代码移至索引操作结果,如下所示:(因为我怀疑您的操作结果 ReadCategory 未被利用)

 public ActionResult Index()
        {
            var dataFile = Server.MapPath("~/App_Data/Category.txt");

            Category passCategory = new Category
            {
                result = "",
                delimiterChar = new[] { ',' },
                userData = System.IO.File.ReadAllLines(dataFile)
            };

            return View(passCategory);
        }


关于c# - 你调用的对象是空的? (异常错误~ASP.NET MVC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61323691/

相关文章:

C# 位图 : "Parameter is invalid" on sizes that aren't a power of 2

c# - HttpRuntime.Cache 对象中缓存键的最大长度?

asp.net - 如何确保同一服务器上托管的应用程序之间不共享 session 状态

asp.net-mvc - 我想调试(在其中设置断点)System.Web.Mvc.DefaultControllerFactory,这可能吗?

c# - Java 1.7 与 C# List/Set/Map 语法糖

c# - 如何使用 Entity Framework 按日期而不是随时间分组

c# - 将 HTML 写入字符串

c# - 在 Web API 中返回特殊的 ASCII 字符

asp.net-mvc - 每种实现的存储库模式优缺点

javascript - jQuery 验证,element.value 在 errorplacement 中未定义