Execute SQL query inside razor without page reload

Hi all,

I currently have a small SQL query which works good on submission.

@{	
  
  if(CurrentContext.Page.IsPostBack){
  	
	var query = "UPDATE Entry_Data SET VoteCounter = VoteCounter + 1 WHERE ID =" + CurrentNode;
    
  }
}

I need a way of executing the above script without page reload and showing a small javascript message shown below. As things stand, the IsPostBack is executed and javascript message ignored.

<input type="Submit" id="vote-btn" value="Vote for me" />
<div class="success">Thanks!</div>
$(function() {
  $("#vote-btn").click(function(e) {
    e.preventDefault();
    $("div.success").fadeIn( 300 ).delay( 1500 ).fadeOut( 400 );
    $(this).addClass("disableVote").delay( 3000 );
  });
});

Any ideas on the best way, easy small script I can use to achieve this?

Thanks, Barry

For anybody interested.
I managed to fix this by adding the script onto the page itself and posting to itself using AJAX.

It turned out I couldn’t execute the file due to a CurrentContext (something in the CMS) and needed to post to itself and add a page.cshtml?vote into the URL to achieve the result, so the postback didn’t reload the page.

The snippet that working for me:

$.ajax({
        method: 'post',
        url: '@path?vote=true',
        
        success: function(data) {
          $("div.success").fadeIn( 300 ).delay( 1500 );
        }
      });

…which then executed the below snippet which was placed further up the page

@{	
  
  if(CurrentContext.Page.IsPostBack){
  	...connection
	var query = "UPDATE Entry_Data SET VoteCounter = VoteCounter + 1 WHERE ID =" + CurrentNode;
    
  }
}

Hopefully this will help somebody else :nerd:

Barry

Just to clarify, slight mistake in my last post :upside_down:

PostBack should be replaced:

@{	
  
  if(CurrentContext.Page.IsPostBack){
  	...connection
	var query = "UPDATE Entry_Data SET VoteCounter = VoteCounter + 1 WHERE ID =" + CurrentNode;
    
  }
}

It should be:

@{	
  
  if (!string.IsNullOrEmpty(Request.QueryString["vote"])){
  	...connection
	var query = "UPDATE Entry_Data SET VoteCounter = VoteCounter + 1 WHERE ID =" + CurrentNode;
    
  }
}

Barry

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.