Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
 
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 9:04
add comment

1 Answer

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?

share|improve this answer
 
I have added my Html also. could you please see that? –  EazyTutor Oct 30 '13 at 9:11
 
I got that using that link. Thanks... –  EazyTutor Oct 30 '13 at 9:22
 
dont forget to mark as answer or upvote if you think it helped. it might also help other users –  Parv Sharma Oct 30 '13 at 9:23
 
I don't that reputation to vote up. surely I will after got that. –  EazyTutor Oct 30 '13 at 9:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.