Trying to add a view that displays a list of items with a particular attribute

时间:2017-06-15 10:32:10

标签: c# asp.net-mvc razor

I am having trouble with an action. I have a list of machines in a database with has a column of dataType 'Bit' which is a serves a checkbox to state whether or not a machine has been delivered to site. I am trying to create a View that displays only items that have been marked as Delivered' but I am getting the below error

[ArgumentException: The parameters dictionary contains a null entry for parameter 'itemDelivered' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult deliveredItems(Boolean)' in 'PreConReports.Controllers.MachineChecklistDBsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

My Full controller and Action for what I am trying to achieve.

public ActionResult deliveredItems(bool deliveredItem)
{
    var machineChecklist = from m in db.machineChecklist
                               select m;
    if (deliveredItem == true)
    {
        machineChecklist = machineChecklist.Where(p => p.itemDelivered);
    }
    else
    {
        return View(machineChecklist.ToList());
    }
    return View(machineChecklist.OrderBy(x => x.machineID));
    {
    }
}

namespace PreConReports.Controllers
{
    [Authorize]
    public class MachineChecklistDBsController : Controller
    {
        private machineChecklistContext db = new machineChecklistContext();

        // GET: MachineChecklistDBs
        public ActionResult Index(string searchString)
        {

            var machineChecklist = from m in db.machineChecklist
                                   select m;
            if (!String.IsNullOrEmpty(searchString))
            {
                machineChecklist = machineChecklist.Where(s => s.machineID.Contains(searchString));
            }
            else
            {
                return View(machineChecklist.ToList());
            }
            return View(machineChecklist.OrderBy(x => x.machineID));
            { 

                }
            }

        //GET: MachineChecklistDBs/itemsDelivered/

        public ActionResult deliveredItems(bool deliveredItem)
        {

            var machineChecklist = from m in db.machineChecklist
                                   select m;
            if (deliveredItem == true)
            {
                machineChecklist = machineChecklist.Where(p => p.itemDelivered);

            }
            else
            {
                return View(machineChecklist.ToList());
            }
            return View(machineChecklist.OrderBy(x => x.machineID));
            {

            }
        }


        // GET: MachineChecklistDBs/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MachineChecklistDB machineChecklistDB = db.machineChecklist.Find(id);
            if (machineChecklistDB == null)
            {
                return HttpNotFound();
            }
            return View(machineChecklistDB);
        }

        // GET: MachineChecklistDBs/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: MachineChecklistDBs/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,machineID,inMel,preCon,preConIssueDate,postCon,postConIssueDate,PUWERrequired,itemDelivered,dateOfDelivery")] MachineChecklistDB machineChecklistDB)
        {
            if (ModelState.IsValid)
            {
                db.machineChecklist.Add(machineChecklistDB);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(machineChecklistDB);
        }

        // GET: MachineChecklistDBs/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MachineChecklistDB machineChecklistDB = db.machineChecklist.Find(id);
            if (machineChecklistDB == null)
            {
                return HttpNotFound();
            }
            return View(machineChecklistDB);
        }

        // POST: MachineChecklistDBs/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,machineID,inMel,preCon,preConIssueDate,postCon,postConIssueDate,PUWERrequired,itemDelivered,dateOfDelivery")] MachineChecklistDB machineChecklistDB)
        {
            if (ModelState.IsValid)
            {
                db.Entry(machineChecklistDB).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(machineChecklistDB);
        }

        // GET: MachineChecklistDBs/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MachineChecklistDB machineChecklistDB = db.machineChecklist.Find(id);
            if (machineChecklistDB == null)
            {
                return HttpNotFound();
            }
            return View(machineChecklistDB);
        }

        // POST: MachineChecklistDBs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MachineChecklistDB machineChecklistDB = db.machineChecklist.Find(id);
            db.machineChecklist.Remove(machineChecklistDB);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的操作的参数为bool deliverItem。

这是一种非可空类型,然后是该操作的强制要求。这就是错误消息所指示的内容。

您可以将方法更改为。

    public ActionResult deliveredItems(bool deliveredItem = false)
    {

        var machineChecklist = from m in db.machineChecklist
                               select m;
        if (deliveredItem == true)
        {
            machineChecklist = machineChecklist.Where(p => p.itemDelivered);

        }
        else
        {
            return View(machineChecklist.ToList());
        }
        return View(machineChecklist.OrderBy(x => x.machineID));
     }

通过提供默认参数,对方法的任何调用都将默认为false,并且不会给出错误。这也意味着您不必在每个链接中提供价值。