0

I have multiple checkbox that are coming from my database. how it looks like Checkbox image. What I wanted is that if I checked one or multiple checkbox than both checked & unchecked checkbox value update my database. If checked it will update active as "1" or unchecked will update as "0".

<input class="form-check-input" value="@dt["MNUD_TEXT"]" ID="@dt["MNUD_TEXT"]" type="checkbox" name="Data"  >

This is the code I prepare the checkbox.

How could I write code in my Action that will help me to update my database as mention.

Please let me know if further information needed.

Nurat Jahan
  • 81
  • 1
  • 9
  • Suggest you refer [Pass List of Checkboxes into View and Pull out IEnumerable](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example of binding to a list of checkboxes –  Sep 10 '18 at 05:26
  • Thank You for your reference but its not that what I am looking for. – Nurat Jahan Sep 10 '18 at 05:50
  • Yes it is! It shows you how to correctly 2-way model bind to your model –  Sep 10 '18 at 05:52
  • suppose I have 30 checkbox with different value. I have selected 20 of them.when I submit this 20 checkbox will update my database `active = 1` and rest 10 unchecked checkbox will update `active = 0` – Nurat Jahan Sep 10 '18 at 05:53
  • Yes I know - which is exactly what the code in the link does! –  Sep 10 '18 at 05:54
  • oh okay . I am trying .............. – Nurat Jahan Sep 10 '18 at 06:00

2 Answers2

0

@foreach(var item in Model){td>@Html.CheckBox("fileItem")@Html.Hidden("fileId",item.ID)}

In action method return list and in view use code which i mentioned above

0

You can do like this, lets say you have check box like,

    <input class="form-check-input" value="@dt["MNUD_TEXT"]" D="@dt["MNUD_TEXT"]" type="checkbox" name="Data" class="clsChkFromDB">

Now you have to bind change event of the checkbox using ".clsChkFromDB" selector and on change event of checkbox. you need to send ajax request which update value of checkbox in database,

        $(function () {
            //Bind on change event with .clsChkFromDB(Checkbox)
            $("#formId").on("change", '.clsChkFromDB', function () {
                updateValue(this.id, this.checked);
            });
        });


        function updateValue(chkId, chkValue) {
            try {
                var strUrl = '@Url.Action("UpdateValue", "ControllerName")';

                var dataObject = JSON.stringify({
                    "chkId": chkId,
                    "chkValue": chkValue
                });

                $.ajax({
                    url: strUrl,
                    type: 'POST',
                    data: dataObject,
                    async: true,
                    contentType: 'application/json',
                    success: function (result) {
                        //Do some action on success
                    },
                    complete: function (result) {
                        //Do some action on complate
                    },
                    error: function (err) {
                        console.error(err);
                    }
                });

            } catch (error) {
                console.error(e);
            }
        }

And finally you have to write below action in your controller,

    [HttpPost]
    public JsonResult UpdateValue(string chkId, string chkValue)
    {
        dynamic result = string.Empty;

        try
        {
            //Code to update value in DB
            result = new
            {
                message = "",
                StatusCode = HttpStatusCode.OK
            };

        }
        catch (Exception ex)
        {
            result = new
            {
                message = ex.Message,
                StatusCode = HttpStatusCode.InternalServerError
            };
        }
       return Json(returnResult);
    }