Reputation: 1285
I've created a site in Webmatrix (cshtml) which uses a loop to display data from an SQL CE table. I have a button which when clicked runs a query to update the 'read' status of that record. I'm having trouble passing the record ID to the query. It works if I set a static ID but when I make the ID dynamic it looks like it is working (and an Alert shows the ID is passing to a variable), but the record doesn't update.
My SQL is:
db.Execute("UPDATE MyTable SET Status='F' WHERE MyTable.MyID=@0", myVar);
And my HTML is (@details. is the record set created for the loop):
<form method="post">
<button type="submit" class="btn btn-xs btn-link pull-right" name="optFlag" onclick="javascript:setStatus(@details.MyID)" ><span class="fa fa-flag"></span></button>
</form>
The JS code I'm using to pass the ID as onclick is:
<script>
function setStatus(myVar) {
}
</script>
Please can someone tell me if I am on the right track and if so what am I doing wrong. If I'm completely off track what is the best way to achieve this?
Many thanks (PS - please let me know by comment/message if this post doesn't meet proper standards so that I can amend without negative score).
Upvotes: 0
Views: 75
Reputation: 1888
You need to rethink your approach. When a page is loaded, the scripting language (razor C#) will execute FIRST before the javascript ever does. So it is impossible to make javascript execute a db call in this way.
You could instead have the javascript load a new page, send the variable to that new page in a querystring or via a form, and then use the value to execute a db call via your razor C# code.
Upvotes: 0