Calling c# method in javascript

Asked

Viewed 722 times

1

I created a method that calls data from a database in C#. How can I call this method in Javascript ?

Follows code

[WebMethod]
    public copa recuperaDados(int cAno)
    {
        var con = ConfigurationManager.ConnectionStrings["Data Source=(localdb)/MSSQLLocalDB;Initial Catalog=C:/USERS/BERNARDO/DOCUMENTS/COPA.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"].ToString();

        copa selecionada = new copa();

        using(SqlConnection myCon = new SqlConnection(con))
        {
            string q = "Select * from copa where ano = @ano";
            SqlCommand cmd = new SqlCommand(q, myCon);
            cmd.Parameters.AddWithValue("@ano", cAno);

            using(SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    selecionada.sede = reader["sede"].ToString();
                    selecionada.campeao = reader["campeao"].ToString();
                    selecionada.vice = reader["vice"].ToString();
                }

                myCon.Close();
            }
            return selecionada;
        }
    }

javascript

<script type="text/javascript">
$("#CopaList").change(function () {
    $("#msg").text("A copa de " + $("#CopaList option:selected").text() + " teve como país(es) sede(s) : " + 

        + "O Campeão foi " +  + "O Vice Campeão foi " + 
       )
})

  • C# does not run on the server and Javascript on the client? I don’t understand how you intend to do this.

2 answers

0

Friend you can use ajax to get the data from the backend without having to refresh the page:

$("#CopaList").change(function () {
  $.ajax({
    type: "GET",
    url: "SuaPagina.aspx/PreencheCampo",
    data: JSON.stringify({ cAno: $("#CopaList option:selected").text() }),
    contentType: "application/json; charset=utf-8",
    success: (response) => {
      $("#msg").text("A copa de " + $("#CopaList option:selected").text() + " teve como país(es) sede(s) : " + response.selecionada.sede + ".O Campeão foi " + response.selecionada.campeao + ".O Vice Campeão foi " + response.selecionada.vice);
    },
    error: (response) => {
      console.log(response);
    }
  })
})

I hope it helps.

  • Your solution looks interesting, but it’s not working in my project. I have to configure something for the project to get ajax support ?

  • What a mistake you’re making friend?

  • The message is not displayed

-1

recovered Data is the name of the backend function. So change the code of @Maycon F. Castro to "Suapagina.aspx/recoveredDados".

But, you need to see the rest of the code. You need to debug it to see if it is entering the recovered functionDados.

In the browser, in the console (no elements) does not generate any errors? If you call directly in the browser by the url "Suapagina.aspx/recuperaDados? cano=2020"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.