【问题标题】:Object Reference not set to an instance of an object? (Exception error ~ ASP.NET MVC)你调用的对象是空的? (异常错误~ASP.NET MVC)
【发布时间】:2020-08-03 00:40:32
【问题描述】:

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

我在视图中使用以下命名空间;

@using ProjectName.Models
@model Category

我的班级模型;

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

我的控制器;

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

任何帮助将不胜感激!

【问题讨论】:

  • 应该是@model类别
  • 抱歉,问题中应该是这样写的。
  • 您确定该视图是由 ReadCategory() 提供的吗?如果是,那么 BeginForm 必须发生一些事情来将值设置回 null。您也可以发布该代码吗?
  • 你指的是哪个代码?抱歉,我不完全确定您的意思?
  • 我不完全确定 BeginForm 是否需要以开始

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


【解决方案1】:

尝试像这样将您的代码移动到您的索引操作结果中:(因为我怀疑您的操作结果 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);
        }


【讨论】:

  • 我刚刚受到 Vassilis 的青睐。谢谢你的作品!
猜你喜欢
  • 1970-01-01
  • 2014-08-26
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 2013-05-27
相关资源
最近更新 更多