0

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).

LB79
  • 1,285
  • 2
  • 12
  • 20
  • how are you passing the value of myVar from your setStatus method to your server ? who is calling the code `db.Execute(` ? – Shyju Jan 20 '16 at 21:41
  • Thanks for the reply. The button is contained within
    and when it submits it checks through conditions to execute the relevant query. if( Request.Form["optFlag"] != null) { db.Execute("UPDATE MyTable SET Status='F' WHERE MyTable.MyID=@0", myVar); }
    – LB79 Jan 20 '16 at 21:43

1 Answers1

0

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.

Timothy Kanski
  • 1,861
  • 14
  • 20