mardi 28 avril 2020

Why is this value returning null to my controller?

I am trying to pass 'true' or 'false' to my ActionResult depending on whether a checkbox had been ticked or not. However, whenever the ActionResult is executed, the following error occurs during runtime:

"The parameters dictionary contains a null entry for parameter 'checkItem' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult Items(System.String, Boolean)' in 'u18043039_HW3.Controllers.ItemsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters"

My view where i am passing the value:

@using (Html.BeginForm("Items", "Items", FormMethod.Post))
             {
                  <input id="isItemChecked" name="checkItem" value="true" type="checkbox" />
             }

My ActionResult:

[HttpPost]
        public ActionResult Items(string ItemDescription, bool checkItem)
        {

            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 + "," + checkItem + Environment.NewLine;

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

            return View();
        }

I am also passing 'string ItemDescription' through to the ActionResult in the exact same way, and this appears to be working fine. Any idea on why this may be?




Aucun commentaire:

Enregistrer un commentaire