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