2

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;

enter image description here

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!

7
  • 1
    Should be @model Category Commented Apr 20, 2020 at 13:33
  • Apologies, was suppose to have been written like so in the question. Commented Apr 20, 2020 at 13:36
  • 3
    are you sure the view is served by ReadCategory() ? if yes, then something BeginForm must happen to set the value back to null. can you post that code as well? Commented Apr 20, 2020 at 13:42
  • Which code are you referring to? Sorry i am not entirely sure on what you mean? Commented Apr 20, 2020 at 13:48
  • I am not entirely sure that BeginForm is even necessary to begin with Commented Apr 20, 2020 at 13:50

1 Answer 1

2

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


Sign up to request clarification or add additional context in comments.

1 Comment

I've just been graced by Vassilis.. thank you this works! <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.