0

I'm new to asp.net mvc and currently using MVC 2. I'm struggling with working with checkboxes for days now. I simply need to get checked checkbox values to be saved in database and on Edit view check them back.

<input type="checkbox"  id="coduit for safety near motor" name="Prepration" value="coduit for safety near motor"/><br />
<input type="checkbox" id="coduit for far side safety" name="Prepration" value="coduit for far side safety"/><br />
<input type="checkbox" id="coduit for power cable to near power point" name="Prepration" value="coduit for power cable to near power point"/><br /> 

On post controller method i can save the values of checked Checkboxes to the database as a comma separated string by using

strign a= = Request.Form["Prepration"];

How can i show them back on Edit view?

I don't know whether this is the way to do this any alternative solution would be great

2 Answers 2

1

The answer of your first question:

Need to get checked checkbox values to be saved in database

On a button click push all the values in a array and from there store them in a hidden field and when you post your form get those values from this hidden field:

    <script type="text/javascript">
     $(document).ready(function () {
         $("input#btnSubmit").click(function () {
         var id = [];
         $("input[name='Prepration']:checked").each(function () {
         id.push($(this).val());
    });
    $("#HiddenFieldId").val(id);
         });
    });
    </script>

Now coming to your second question:

How can i show them back on Edit view?

<input type="radio" id="a" name="Prepration" checked="@Model.BoolPropertyName" />

Here you can have the value of in boolan.

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

0

you can client side solution,

var data="";
$.each($("input:checkbox"),function(){
    if($(this).is("checked")){
        data+= $(this).val();
    }
});

// post here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.