Lai32290
Lai32290

Reputation: 8548

How to get POST data inside a C# WebMethod?

I need to use a jQuery ajax request to post values for processing in C# code behind. I can send data but I don't know how to retrieve it from inside the C# WebMethod.

var cliente;
$.post(
    "../Dados/GetDados.aspx/GetClienteById",
    {
        Id: id
    },
    function(dado) {
        cliente = dado;
    });

And the C# code:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetClienteById()
{
    int PostId = What_can_I_put_here?;

    string dados = "null";

    using (SysContext db = new SysContext())  // I'm using Entity Framework
    {
        if (db.Clientes.Count(r => r.Id == PostId) > 0)
            dados = new JavaScriptSerializer().Serialize(db.Clientes.Where(r => r.Id == PostId).ToList());
    }
    return dados;
}

Upvotes: 1

Views: 2933

Answers (1)

Timeout
Timeout

Reputation: 7909

Add it as a parameter.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetClienteById(int id)

Upvotes: 2

Related Questions