Error sending data by AJAX to Method in ASP.NET

0

Hello friends, I'm working with ASP.NET / C # and I'm trying to pass a parameter through AJAX ... but I get this error, I recently published a very similar question, where I did the same but from a ActionLink but as I am now passing the parameter by js I imagine that I have to be wandering in another part. Thanks to all the beautiful and brilliant people of Stack ... I love you: D

this is my mistake:

this is what I am trying to do ...

$(document).ready(
  function() {
    //getting id parameter value
    var selects = document.getElementById("id_req").value;
    var id_x = parseInt(selects);
    alert(id_x);

    //ajax man u.u_-
    $.ajax({
      type: 'GET',
      url: '@Url.Action("UniqueRequest", "Request")',
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: {
        //al parecer mi error está porque asp.net no reconoce mi id
        // la solución en asp.net era algo así:
        //  (new { id = 123 },null) ... ya lo probé aquí en js y nada u.u
        'id': id_x
      },
      //response ...
      success: function(response) {
        RenderizarInfo(response.message);
      },
      failure: function(response) {
        alert("something get wrong u.u ..... " + response.message);
      }
    });
  }
);

this is my model

using System.ComponentModel.DataAnnotations;

namespace PCotiza_compras.Models {
  public class Requests {

    public string wiw {
      get;
      set;
    }

    [Display(Name = "Id")]
    public int id {
      get;
      set;
    }

    [Display(Name = "Proyecto")]
    public string nameproject {
      get;
      set;
    }

    [Display(Name = "Esquema")]
    public int schemeid {
      get;
      set;
    }

    [Display(Name = "Fecha de solicitud")]
    public string createddate {
      get;
      set;
    }

    [Display(Name = "Respuesta Fecha")]
    public string answerdate {
      get;
      set;
    }

    [Display(Name = "Estado")]
    public int status {
      get;
      set;
    }

    [Display(Name = "Días de expiración")]
    public string expiration {
      get;
      set;
    }

    [Display(Name = "Email")]
    public string email {
      get;
      set;
    }

    [Display(Name = "Departamento")]
    public int departmentid {
      get;
      set;
    }

    [Display(Name = "Categoría")]
    public int categoriesid {
      get;
      set;
    }

    [Display(Name = "Visto u.u_r")]
    public int seen {
      get;
      set;
    }

    [Display(Name = "Visto Fecha")]
    public string seendate {
      get;
      set;
    }

    [Display(Name = "Ediciones")]
    public int edit {
      get;
      set;
    }

    [Display(Name = "Rechazado")]
    public int rejected {
      get;
      set;
    }

    [Display(Name = "Formato")]
    public byte[] formart {
      get;
      set;
    }

    [Display(Name = "Tipo de archivo")]
    public string ftype {
      get;
      set;
    }

    [Display(Name = "Nombre de archivo")]
    public string fname {
      get;
      set;
    }

    [Display(Name = "Tamaño")]
    public int fsize {
      get;
      set;
    }

  }
}

this is my method

[WebMethod]
public JsonResult UniqueRequest(int idx) {

  string cs = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
  string sql = "SELECT [nameproject],[wiw],[email],[schemeid],[status] FROM [DB_PCC].[dbo].[Requests]" +
    "WHERE [id] = '" + idx + "' ";

  using(SqlConnection con = new SqlConnection(cs)) {
    SqlCommand cmd = new SqlCommand(sql, con);

    con.Open();
    List < Requests > req = new List < Requests > ();
    using(SqlDataReader rdr = cmd.ExecuteReader()) {
      while (rdr.Read()) {
        req.Add(new Requests() {
          wiw = rdr.GetString(rdr.GetOrdinal("wiw")),
            nameproject = rdr.GetString(rdr.GetOrdinal("nameproject")),
            email = rdr.GetString(rdr.GetOrdinal("email")),
            status = rdr.GetInt32(rdr.GetOrdinal("status")),
            schemeid = rdr.GetInt32(rdr.GetOrdinal("schemeid")),
            departmentid = rdr.GetInt32(rdr.GetOrdinal("departmentid")),
            categoriesid = rdr.GetInt32(rdr.GetOrdinal("categoriesid")),
            seen = rdr.GetInt32(rdr.GetOrdinal("seen")),
            edit = rdr.GetInt32(rdr.GetOrdinal("edit")),
            rejected = rdr.GetInt32(rdr.GetOrdinal("rejected"))

        });
      }
      con.Close();
      return Json(new {
        success = true, message = req
      }, JsonRequestBehavior.AllowGet);
    }

  }




}
    
asked by E.Rawrdríguez.Ophanim 21.12.2017 в 17:04
source

2 answers

0

My mistake was in my AJAX I have:

$.ajax({
    type: 'GET',
    url: '@Url.Action("UniqueRequest", "Request")',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',                  
    data: { 'id': id_x },

And my method expects a idx :

[WebMethod]
public JsonResult UniqueRequest(int idx)
{ 
     .....
}

So I change my AJAX id : x by idx : x and you're done.

    
answered by 21.12.2017 / 23:32
source
2

In the ajax data, remove the parentheses. And Change the @ Url.Action by the static route.

$.ajax({
      type: 'GET',
      url: '/Request/UniqueRequest',
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: {'id': id_x },
      //response ...
      success: function(response) {
        RenderizarInfo(response.message);
      },
      failure: function(response) {
        alert("something get wrong u.u ..... " + response.message);
      }
    });

And change your type of method in the controller by ActionResult

public ActionResult UniqueRequest(int idx) { ...}
    
answered by 21.12.2017 в 17:14