1

I have to get the checkbox form collection as bool value array to insert into bit datatype column in SQL Server. I tried the below thing with partial luck.

If all the checkboxes are checked, It is working fine. But If some left as unchecked only the checked items are in array. So the index varies and could not identify which belongs to which record?

Please help me.

string[] names = Students["name"].Split(char.Parse(","));
            string[] dnos = Students["dno"].Split(char.Parse(","));
            string[] adds = Students["address"].Split(char.Parse(","));
            string[] actives = Students["active"].Split(char.Parse(","));


            for (var i = 0; i < names.Length; i++)
            {
                student stds = new student();
                stds.name = names[i];
                stds.dno = dnos[i];
                stds.address = adds[i];
                if (actives[i] == "on")
                    stds.active = true;
                else
                    stds.active = false;
                db.students.AddObject(stds);

            }
            db.SaveChanges();

HTML:

@model DynamicAddition.DAL.student

@{
ViewBag.Title = "Create";
}

<h3>Create</h3>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<form id="formstudent" method="post">
    <table id="formTable">
        <thead>
        <tr><th>Name</th><th>D. No</th><th>Address</th><th>Active</th><th>Add New</th></tr>
        </thead>
        <tbody>
        <tr>
            <td><input type="text" name="name" /></td>
            <td><input type="text" name="dno" /></td>
            <td><input type="text" name="address" /></td>
            <td><input type="checkbox" name="active" /></td>
            <td><a href="javascript:addRow();"><b>Add new</b></a></td>
        </tr>
        </tbody>
    </table>
    <p><input type="submit" value="Create" /></p>
    </form>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="../../Scripts/jquery-1.5.1.js"></script>
<script language="javascript" type="text/javascript">
function addRow() {
    $('#formstudent tbody tr:first').clone().find("input").each(
    function () {
        $(this).val = '';
    }).end().appendTo("#formTable");
}
</script>
Arockia
  • 440
  • 1
  • 6
  • 18
  • Can you post the client-side code as well? It seems you form post is incomplete. Perhaps you disable the checkboxes? – Stefan Oct 30 '13 at 09:04

1 Answers1

1

i stumbled to the same problem while creating a dynamic form.

going by the html standards there are some controls that are posted back and some that are not. So a checkbox that is not checked or an input that is disabled isnt posted back to the server on a form postback.

The normal practive to handle these situations is to make a hidden input with the default value in you case since the default value is false make a input

@Html.Hidden("name1",..)

and set the value as false then using the same name as of this input make a checkbox

@Html.Checkbox("name1", ..)

by doing this even if the checkbox isnt checked the value of the hidden input will be posted back to the server and you will get a valid false value.

**Note :- Asp.net MVC uses same trick whenever is needs a value postback. **Note:- there is already an answer depicting this nicely how to POST/Submit an Input Checkbox that is disabled?

Community
  • 1
  • 1
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80