简体   繁体   中英

Object Reference not set to an instance of an object? (Exception error ~ ASP.NET MVC)

Currently i am using variables inside my view that have been declared within a model, and assigned values within the respective controller. I am using Model.variableName to reference these variables, however the following exception is thrown during debugging;

在此处输入图像描述

I am using the following namespaces within my view;

@using ProjectName.Models
@model Category

My class model;

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

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

Any help would be greatly appreciated!

Try moving your code to your index action result like this: (as I suspect your action result ReadCategory is not being utilized)

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


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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