0

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>           
1
  • Could you remove [Bind(Include = "Id,ReaderId,BookId")], and see it work? Commented Jan 9, 2017 at 17:22

1 Answer 1

2

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

[Bind(Include = "Id,ReaderId,BookId")]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.