0

i'm new to asp.net mvc and i'm trying to handle saving and retrieving the checkBox checked values.

In my view i'm having below checkboxes

<input type="checkbox" id="cabling in coduit" name="Prepration" value="cabling in coduit"/>cabling in coduit<br />
<input type="checkbox" id="Power to be done by liteStart" name="Prepration" value="Power to be done by liteStart"/>Power to be done by liteStart<br />
<input type="checkbox" id="Other - pls spec" name="Prepration" value="Other - pls spec"/>Other - pls spec<br />

In my controller:

String vals=Request.Form["Prepration"];

I get the checked values as a comma separated string and i save that string into my database column.

Now when i retrieve the value i have something like below.

cabling in coduit,Power to be done by liteStart

How can i check the relevant checkboxes on my edit view using the comma separated string

1 Answer 1

0

then you can do something like this

    if(vals.indexOf("cabling in coduit") > -1 ){
       $('#cabling in coduit').prop('checked', true);
    }
    else if(vals.indexOf("Power to be done by liteStart") > -1 ){
       $('#Power to be done by liteStart').prop('checked', true);
    }
    else if(vals.indexOf("Other - pls spec") > -1 ){
       $('#Other - pls spec').prop('checked', true);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

The code supplied is javascript, and the lines that start with $ use jQuery. You would put this code in the $(document).ready() function and it would restore the checkbox state for you. You could read more about jQuery to get the idea.

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.