简体   繁体   中英

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>           

您故意将IfWantEmail排除在将POST数据绑定到模型之外。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM