Kasparas
Kasparas

Reputation: 89

Getting value from CheckBox in asp.net mvc 5

I am trying to get a CheckBox value from the view, however, even if i check the CheckBox it still returnse me "false".

What am i doing wrong?

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,ReaderId,BookId")] BookingViewModel bookingViewModel)
        {
            Booking booking = new Booking();
            if (ModelState.IsValid)
            {
                var userId = User.Identity.GetUserId();
                int readerId = db.Readers.Where(c => c.ApplicationUserId == userId).First().ReaderId;




                booking.ReaderId = readerId;
                booking.BookId = bookingViewModel.BookId;


                db.Bookings.Add(booking);

                bool value = bookingViewModel.IfWantEmail;
                db.SaveChanges();

                return RedirectToAction("ShowUserBookings");
            }

            ViewBag.BookId = new SelectList(db.Books, "BookId", "Title", booking.BookId);

            return View(booking);
        }

And this is the code from my view:

            <dt>
                @Html.DisplayNameFor(model => model.IfWantEmail)
            </dt>

            <dd>
                @Html.CheckBoxFor(model => model.IfWantEmail)
            </dd>           

Upvotes: 0

Views: 1080

Answers (1)

Paul Abbott
Paul Abbott

Reputation: 7211

You're intentionally excluding IfWantEmail from binding the POST data to the model.

[Bind(Include = "Id,ReaderId,BookId")]

Upvotes: 2

Related Questions