简体   繁体   中英

CheckBox with Comma Separated string values

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

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM