0
votes

within my model i have a bool value called itemCheck:

public class Item
    {       
        public Array userDataItems { get; set; }

        public char[] delimiterChar { get; set; }

        public bool itemCheck { get; set; }

    }

My viewModel, linking the item model to the controller:

public class CategoryItemViewModel
    {
        public Item ItemList { get; set; }

        public Category CategoryList { get; set; }
    }

Which i then initialize within my main controller as false:

public ActionResult Index()
        {
            CategoryItemViewModel CIVM = new CategoryItemViewModel();
            CIVM.ItemList = GetItemModel();
            CIVM.CategoryList = GetCategoryModel();
            return View(CIVM);
        }

 public Item GetItemModel()
        {
            var dataFileItems = Server.MapPath("~/App_Data/Item.txt");

            Item iModel = new Item()
            {
                userDataItems = System.IO.File.ReadAllLines(dataFileItems), //Items Text File
                delimiterChar = new[] { ',' },
                itemCheck = false,
            };

            return iModel;
        }

Then i use it in my view as the bool value to indicate whether a checkbox is ticked or not :

@using (Html.BeginForm("Items", "Items", FormMethod.Post, new { id = "formFieldTwo" }))
        {
            @Html.CheckBoxFor(m => m.ItemList.itemCheck, false)
        }

And lastly i try to access this variable 'itemCheck' which should be set to 'false' within my ActionResult:

[HttpPost]
        public ActionResult Items(string ItemDescription, CategoryItemViewModel m)
        {

            bool originalValue = m.ItemList.itemCheck;

            var FkFile = Server.MapPath("~/App_Data/ForeignKeyValue.txt");

            var Fk = System.IO.File.ReadAllText(FkFile);

            var dataFileItems = Server.MapPath("~/App_Data/Item.txt");

            var numberOfLinesItems = System.IO.File.ReadLines(dataFileItems).Count();

            var textFileDataItems = ItemDescription + "," + numberOfLinesItems + "," + Fk + "," + originalValue + Environment.NewLine;

            System.IO.File.AppendAllText(dataFileItems, textFileDataItems);

            return View();
        }

However, i get the following error on the line 'bool originalValue = m.ItemList.itemCheck;' :

" System.NullReferenceException: 'Object reference not set to an instance of an object.' "

I fail to understand why my program is giving me this error?

1
What is ItemList in the statement m=>m.ItemList.itemCheck? Your model shows that the property containing itemCheck is called iModel. Shouldn't this be m=>m.iModel.itemCheck? You're missing a piece of the puzzle in your code examples. I would wager you are sending a list of these Items to the view and trying to use the Html editors for each item in the list. There are other ways that you have to use to access the properties of a collection of items in a view model like you are wanting. - Tommy
Apologies, i have uploaded another part to my models in the question, the reason i have done this is to have the ability to use variables from multiple models within a single view. - SeventhWarhawk
@Tommy i have updated my question with hopefully what is required for you to see what is going on. I apologize if i am a bit uncertain of any of your suggestions. ASP.NET and MVC are both very new concepts to me. - SeventhWarhawk

1 Answers

0
votes

Change :

 @Html.CheckBoxFor(m => m.ItemList.itemCheck, false)

To :

 @Html.CheckBoxFor(Model => Model.ItemList.itemCheck, false)