Return DB values via ajax

1

I have a screen where I should populate with database values using ajax.

When I click the button to fill in the fields the following error appears:

  

Failed to load resource: the server responded with a status of 500   (Internal Server Error)

I do not know what's going on.

Model Code:

public class Client
{
    [Key]
    [MaxLength(128)]
    public string client_id { get; set; }

    [MaxLength(20)]
    public string client_cnpj { get; set; }

    [MaxLength(255)]
    public string client_companyname { get; set; }

    [Required]
    [MaxLength(255)]
    public string client_fantasyname { get; set; }

    [Required]
    [MaxLength(128)]
    public string client_namerepresentative { get; set; }

    [Required]
    [MaxLength(15)]
    public string client_cep { get; set; }

    [Required]
    [MaxLength(255)]
    public string client_street { get; set; }

    [Required]
    [MaxLength(10)]
    public string client_number { get; set; }

    [MaxLength(55)]
    public string client_complement { get; set; }

    [Required]
    [MaxLength(128)]
    public string client_neighborhood { get; set; }

    [Required]
    [MaxLength(128)]
    public string client_city { get; set; }

    [Required]
    [MaxLength(2)]
    public string client_uf { get; set; }

    [MaxLength(128)]
    public string client_email { get; set; }

    [MaxLength(15)]
    public string client_phone1 { get; set; }

    [MaxLength(15)]
    public string client_phone2 { get; set; }

    public int? client_currentcarrier { get; set; }

    public int? client_voice { get; set; }

    public int? client_web { get; set; }

    public int? client_renegotiation { get; set; }

    public DateTime? client_datevisitorcontact { get; set; }

    [Required]
    public string client_note { get; set; }

    public int? client_statusnegotiation { get; set; }

    [Required]
    [MaxLength(128)]
    public string client_company_id { get; set; }

    [MaxLength(128)]
    public string client_supervisingconsultant { get; set; }

    [MaxLength(128)]
    public string client_supervisingsalessupport { get; set; }

    [MaxLength(128)]
    public string client_consultant { get; set; }

    [MaxLength(128)]
    public string client_salessupport { get; set; }

    [MaxLength(128)]
    public string client_source { get; set; }

    public bool client_return { get; set; }

    public bool client_sendemail { get; set; }

    public DateTime client_dateregister { get; set; }

    [ForeignKey("client_company_id")]
    public virtual Company Company { get; set; }

    public virtual ICollection<HistoricFunnel> HistoricFunnel { get; set; }

    public virtual ICollection<ActivityClient> ActivityClient { get; set; }
}

Controller code:

[HttpPost]
public JsonResult ReturnVisit(string id)
{
    var visit = _clientAppService.GetById(id);
    return Json(new { visit = visit });
}

JavaScript code:

$(".visits-table tr a").click(function () {
    var id = $(this).parent().parent().attr("id");

    $.ajax({
        dataType: "json",
        type: "POST",
        url: "/Visits/ReturnVisit",
        data: { id: id },
        success: function (data) {
            alert("entrou")
            alert(data)
        }
    });

    return false;
});
    
asked by anonymous 18.10.2016 / 20:13

3 answers

1

Error 500 is a generic HTTP error, you can not tell what is happening. You need to read the page with the result of the request. Also, if your IIS is not configured to show the errors it will not resolve. Try configuring IIS to turn off customErrors by configuring in Web.onfig

<system.web>
    <customErrors mode="Off"></customErrors>
</system.web>

Then run your code with error and check exactly what error is occurring. If you are using to debug Firefox, use Firebug and see the Request Ajax result in the Console tab. Chrome is the same thing. To enter this console, type F12.

Another thing, it looks like you're trying to return a domain class "visit." You should never expose a domain class, always transform your domain class into a ViewModel class (POCO). This avoids cyclical reference and other problems when exposing a domain class.

    
18.10.2016 / 20:57
0

Try to do by GET

$(".visits-table tr a").click(function () {
var id = $(this).parent().parent().attr("id");

$.ajax({
    dataType: "json",
    type: "GET",
    url: "/Visits/ReturnVisit",
    data: { id: id },
    success: function (data) {
        alert("entrou");
        alert(data);
    }
});

return false;

});

public JsonResult ReturnVisit(string id)
{
    var visit = _clientAppService.GetAll();
    return Json(visit,JsonRequestBehavior.AllowGet);
}
    
18.10.2016 / 20:22
0

Depending on your application's IIS publishing, placing the direct URL of controller/action may not be legal. Use the power of razor .

$(".visits-table tr a").click(function () {
    var id = $(this).parent().parent().attr("id");

    $.ajax({
        dataType: "json",
        type: "POST",
        url: '@Url.Action("ReturnVisit", "Visits")',
        data: { id: id }
        success: function (data) {
            alert("entrou")
            alert(data)
        }
    });

    return false;
});

Be careful in the requests ... if the controller method is written as [HttpGet] you must make a GET request. If it is as [HttpPost] you must make a POST request.

Your controller

[HttpPost]
public JsonResult ReturnVisit(string id)
{
    var visit = _clientAppService.GetAll();
    return new JsonResult { Data = visit };
}

Your template must be [Serializable] :

using System;

[Serializable]
public class Client
{

....

}

Any property that is a Class, this class has to be serialized as well.

More about serialization

    
18.10.2016 / 20:35