I am having problems in showing the data in Edit form - it always show checkbox as unchecked although in database it is a true value. I have used various formats but no success
Database: Project table with field 'Active' with bit data type.
projectId = UrlData[0];
var sql = "SELECT * FROM Projects WHERE ProjectID = @0";
var project = db.QuerySingle(sql, projectId);
var active= project.Active.ToString(); //(Also gets error if it was a null value
<input type="checkbox" name="active" value="@active" @Helpers.Checked("active","true") /> (have copied the code into Helpers.cshtml file from Mike's book
<input type="checkbox" @(item.active== true ? "checked": null) /> (this works in WebGrid)
SHR
Member
30 Points
63 Posts
Checkbox problem in Edit page
May 14, 2012 09:50 PM|LINK
Hi
I am having problems in showing the data in Edit form - it always show checkbox as unchecked although in database it is a true value. I have used various formats but no success
Database: Project table with field 'Active' with bit data type.
projectId = UrlData[0];
var sql = "SELECT * FROM Projects WHERE ProjectID = @0";
var project = db.QuerySingle(sql, projectId);
var active= project.Active.ToString(); //(Also gets error if it was a null value
in order to show it in an Edit form have tried:
<input type="checkbox" name="active" @(Request["active"] == "on" ? " checked=\"checked\"" :"") /> <br/>
<input type="checkbox" name="active" value="@active" @Helpers.Checked("active","true") /> (have copied the code into Helpers.cshtml file from Mike's book
<input type="checkbox" @(item.active== true ? "checked": null) /> (this works in WebGrid)
Please help with some explanation as well please.
Many thanks
Hamid
GmGregori
Contributor
5448 Points
736 Posts
Re: Checkbox problem in Edit page
May 14, 2012 10:29 PM|LINK
Try with the following code:
@{ projectId = UrlData[0]; var sql = "SELECT * FROM Projects WHERE ProjectID = @0"; var db = Database.Open("..."); // Insert your db name var project = db.QuerySingle(sql, projectId); var active = ""; if (project.Active != null){ active = project.Active.ToString(); } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <form> <input type="checkbox" name="active" value = "True" @(active == "True" ? " checked=\"checked\"" :"") /> <br/> </form> </body> </html>SHR
Member
30 Points
63 Posts
Re: Checkbox problem in Edit page
May 15, 2012 02:37 AM|LINK
It works
Many thanks Gm