1

I have a checkbox control in a view of m MVC application. How can I check if it is checked or not in a controller? Is this possible?

user8811
  • 75
  • 1
  • 2
  • 12
  • 1
    possible duplicate of [Getting Checkbox Value in ASP.NET MVC 4](http://stackoverflow.com/questions/14730746/getting-checkbox-value-in-asp-net-mvc-4) – Rune Aug 25 '14 at 21:51
  • Are you binding the checkbox to a property? –  Aug 25 '14 at 23:59
  • NO, I'm not..I just want to check if the box is checked or not in a controller. – user8811 Aug 26 '14 at 00:23
  • A little more context would be useful. What does your current view, model and controller look like? On a high level, you'll need to bind the value of the checkbox to a property on your model, but how to best do that needs more context. – Martijn Aug 26 '14 at 12:34

1 Answers1

3

You should use a Boolean value as the parameter to CheckBox to indicate the checked status and get the ids of the selected check boxes and pass to the controller

Code in View :

@model checkbox_app.Models.CheckboxModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>

    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script>
        $(document).ready(function () {

            $("#checkbox").change(function ()
            {
                var n = $(this).is(':checked');
                if ($(this).is(':checked')) {
                    $.ajax({
                        url: '@Url.Action("Index1", "Check")',
                        data: { check: n },
                        type: 'GET',
                        datatype: "json",
                        contentType: 'application/json; charset=utf-8',
                        async: false,
                        success: function (data) {
                            alert(data.results);
                        }
                    });
                }
            });            
        });
    </script>
</head>
<body>  
        <div class="editor-field fieldwidth floatL">
            @Html.CheckBoxFor(x => Model.checkCntrl, new { id = "checkbox"})
        </div> 
</body>
</html>

Code in Model :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace checkbox_app.Models
{
    public class CheckboxModel
    {
        public bool checkCntrl { get; set; }

    }
}

Code in Controller :

using checkbox_app.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace checkbox_app.Controllers
{
    public class CheckController : Controller
    {
        //
        // GET: /Check/

        public ActionResult Index()
        {

                return View();


        }

        public ActionResult Index1(bool check)
        {

            if (check)
            {
                string str = "done";
                 return Json(new { results = str }, JsonRequestBehavior.AllowGet);

            }
            else
            {
                return View("Error");
            }
        }

    }
}