I have a class Localidad
that contains an attribute of the class Provincia
, when I want to do nested combos by Ajax with Jquery it gives me:
error 500 - Internal server error-.
If I remove the attribute Provincia
, it's perfect. How can i fix this?
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace javaScript.Models
{
public class Localidad
{
public int Id { get; set; }
public string Descripcion { get; set; }
[Display(Name ="Provincia")]
public int IdProvincia { get; set; }
public Provincia Provincia
{
get { return new Provincia(this.IdProvincia); }
set { }
}
public void set_Provincia(int idProvincia)
{
this.Provincia = new Provincia(1);
}
}
}
namespace javaScript.Models
{
public class Provincia
{
// public int Id { get; set; }
public int Id { get; set; }
public string Descripcion { get; set; }
public string Gobernador { get; set; }
public List<Localidad> Localidades { get; set; }
}
}
Method in the Controller to return the list:
public ActionResult GetSecondValues(string IdProvincia)
{
IEnumerable<Localidad> localidadList = null;
var db = new Contexto();
var id = Int32.Parse(IdProvincia);
if (id > 0)
{
//se carga la lista de Localidades
localidadList = db.Localidades.Where(x => x.IdProvincia == id).ToList();
}
var retorno = Json(localidadList);
return retorno;
}
Ajax method on the view:
<script type = "text/javascript">
$(function () {
$("#IdProvincia").change(function () {
var selectedItem = $(this).val();
var ddlStates = $("#IdLocalidad");
var statesProgress = $("#states-loading-progress");
statesProgress.show();
$.ajax({
cache: false,
type: "POST",
url: "@(Url.Action(" GetSecondValues "))",
data: {
"IdProvincia": selectedItem
},
success: function (data) {
ddlStates.html('');
$.each(data, function (id, option) {
ddlStates.append($('<option></option>').val(option.Id).html(option.Descripcion));
});
statesProgress.hide();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve states.');
statesProgress.hide();
}
});
});
});
</script>
The problem arises from a Recursion between the Provinces class that contains a List of Localities and the Localities class that Contains an attribute of the Province type The strange thing is that the error jumps in the ajax method and not in Visual Studio, which makes it difficult for me to find the error , because ajax was only telling me Error 500 Internal server error and I don't have much experience in JS. Removing either of the two attributes (the list of localities in province, or the province in localities) the code works correctly.
From what I understand, the error you're getting would be Cannot implicitly convert type X to 'System.Collections.Generic.IEnumerable<Locality>' .
This would be because the type returned by db.Localities cannot be converted directly to a list of Localities .
You could try the following:
A problem I see is that you are using
Int32.Parse
to convert the stringIdProvincia
, but if it becomes null it will send you aArgumentNullException
, this generates an exception and therefore the 500 error makes sense, so the ideal would be to use theConvert.ToInt32
.Ideally, you'll want to change your Controller code to the following:
But, seeing the result you use it to fill a drop-down list, I would recommend generating the HTML from the Controller:
Finally, in the Ajax call, it would only be necessary to render that HTML in the desired control: