1

I have multiple check box values in a form. I am serializing the form and send to mvc controller as JSON data. How can i de-serialize the check box values?

This is my html -

@using (Html.BeginForm("SaveOfficeConfiguration", "Offices", FormMethod.Post, new { Id = "frmOfficeConfigSave" }))
{  
<div id="divOfficeForm">
    <div style="width: auto; height: auto; border: 3px outset silver;">
        <table class="mainsectionable" width="100%">
            <thead>
                <tr>
                    <th style="text-align: center;">
                        <center>KCCM Profile Access</center>
                    </th>
                </tr>
                <tr>
                    <td>
                        @using (Html.BeginForm())
                        {
                            IEnumerable<SelectListItem> Brands = ViewBag.GetBrands;
                            foreach (var item in Brands)
                            {
                            @Html.CheckBox("KCCM_Brands", false, new
                                                        {
                                                            value = item.Value
                                                        });
                            <label>@item.Text</label><br />
                                            }
                                        }
                    </td>
                </tr>
            </thead>
        </table>
    </div>
</div>
}

This is my javascript function-

function SaveOfficeConfigNew() {
    var officeID = $('input[name="hdnOfficeID"]').val();
    var url = "/OfficeManagement/Offices/SaveOfficeConfiguration?officeID=" + officeID;
    ShowWait();
    $.ajax({
        type: "POST",
        url: url,
        data: frmOfficeConfigSave.$('input').serialize(),
        success: function (data) {
            HideWait();
            alert(data.msg);
        },
        error: function (data) {
            HideWait();
            alert(data.msg);
        }
    });
    applyFilter();
    return false;
}

This is my gonna be controller action -

[HttpPost]
    public ActionResult SaveOfficeConfiguration(int ? officeID, FormCollection form)
    {
        try
        {
            */..............
              ..............*/
            return Json(new
            {
                success = true,
                msg = String.Empty,
                id = 1

            }, JsonRequestBehavior.AllowGet);
        }
        catch (Exception error)
        {
            return Json(new
            {
                success = false,
                msg = error.Message,
                id = -1
            }, JsonRequestBehavior.AllowGet);

        }
    }
Sandy
  • 2,429
  • 7
  • 33
  • 63

3 Answers3

0

You simply have to receive a List<string> parameter, with the same name you're giving the checkboxes, i.e., KCM_Brands. The Model Binder will deserialize it directly for you.

[HttpPost]
public ActionResult SaveOfficeConfiguration(int ? officeID, FormCollection form,
  List<string> KCM_Brands)
{
    ....
}

To serialize your form data to post it, use the function suggedted in this post:

JSON object post using form serialize not mapping to c# object

Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Its `null`, any issues in my javascript function? – Sandy Sep 20 '13 at 08:07
  • I didn't check your js. And yes, it's wrong. I thoght you had checked what was being sent with the alerts. Look ad the reference I've added – JotaBe Sep 20 '13 at 08:19
0

You can use the FormsCollection to retrieve the check box values:

Controller:

[HttpPost]
public ActionResult SaveOfficeConfiguration(int ? officeID, FormCollection form) 
{
var CheckBoxValues = form["KCCM_Brands"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x=>int.Parse(x));
}

For More info Have a look here : http://www.mindstick.com/Articles/2ee905ba-aea1-4d83-a49d-d8d8fb10c747/?Checkbox%20control%20in%20MVC

Jatin patil
  • 4,252
  • 1
  • 18
  • 27
0

Instead of

@Html.CheckBox("KCCM_Brands", false, new
{
    value = item.Value
});

use this code to generate checkboxes

<input type="checkbox" name="KCCM_Brands" value="@item.Value" />

The action on your controller should look like this

public ActionResult SaveOfficeConfiguration(int? officeID, List<string> KCCM_Brands) 

When you post your form, List<string> KCCM_Brands will be populated only with the values of selected checkboxes.

Also, I don't know if your javascript is correct, but I had to make the following change for it to work

data: $('#frmOfficeConfigSave input').serialize()
Ovidiu
  • 1,407
  • 12
  • 11