sql - Checkbox hell - webmatrix/razor -


i'm having trouble checkboxes on site.

here's code have far:

@{ layout = "~/_sitelayout.cshtml";  websecurity.requireauthenticateduser();   var db = database.open("stayinflorida");  var rpropertyid = request.querystring["propertyid"];  var propertyinfo = "select * propertyinfo propertyid=@0"; var qpropertyinfo = db.querysingle(propertyinfo, rpropertyid);  if(ispost){ var sql = "insert propertyinfo (fepool, fejac, fegames) values (@0, @1, @2) propertyid=@3"; db.execute(sql, request["check1"], request["check2"], request["check3"], rpropertyid); } }  <form method="post">  <div> <input type="checkbox" name="check1" value="true"  />    value 1 <br/> <input type="checkbox" name="check2" value="check2" />    value 2 <br/> <input type="checkbox" name="check3" value="check3" />    value 3 <br/>   <div> <input type="submit" name="buttonsubmit" value="submit" /> </div> </form> 

what need able check 3 columns in database, , see if set true (the columns set "bit"). if are, want checkbox shown checked. want able toggle these checkboxes , post "true" or "false" using form, post database.

hopefully makes sense?

your appreciated. thanks, gavin

you have where clause in insert statement makes no sense. if inserting, create new row. suspect intend update existing row in case sql should be:

"update propertyinfo set fepool = @0, fejac= @1, fegames = @2 propertyid=@3" 

if checkbox not checked, nothing included checkbox in request.form collection. if checked, value passed "on" default, or whatever have specified in "value" attribute. check1, "true". check2, "check2". need establish whether checkbox included in request.form collection , set values want commit database accordingly. if want pass true or false depending on whether checkbox checked, can this:

var c1 = !request["check1"].isempty(); var c2 = !request["check2"].isempty(); var c3 = !request["check3"].isempty(); var sql = "update propertyinfo set fepool = @0, fejac= @1, fegames = @2 propertyid=@3"; db.execute(sql, c1,c2,c3,rpropertyinfo); 

now have series of booleans, can use determine whether checkbox checked or not using conditional attributes:

<input type="checkbox" name="check1" checked="@c1" /> value 1<br/> <input type="checkbox" name="check2" checked="@c2" /> value 2<br/> <input type="checkbox" name="check3" checked="@c3" /> value 3<br/> 

if want preset values of checkboxes query, prior ispost block:

var rpropertyid = request.querystring["propertyid"]; var propertyinfo = "select * propertyinfo propertyid=@0"; var qpropertyinfo = db.querysingle(propertyinfo, rpropertyid); bool c1 = qpropertyinfo.fepool; bool c2 = qpropertyinfo.fejac; bool c3 = qpropertyinfo.fegames; 

obviously don't need var in front of c1 - c3 variables in ispost block now.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -