MVC 5, DropDownList cascading

1

I have the following problem: suddenly, my solution is the one you are looking for in the part of the controller that is needed for your model. I have the following problem, what happens is that if I give the apartment and then the city, the first time I get it right but when going to record, it does not allow me since it tells me that it could not save a value of zero (what ever ), I am judicious taking the lessons but I have not been able to solve this part.

        @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
        $(document).ready(function () {
            $("#DepartamentID").change(function () {
                $("#CityID").empty();
                $("#CityID").append('<option value="0">[Select a City]</option>');
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("GetCities")',
                    dataType: 'json',
                    data: { departmentId: $("#DepartamentID").val() },
                    success: function (data) {
                        $.each(data, function (i, data) {
                            $("#CityID").append('<option value="'
                             + data.departmentId + '">'
                             + data.Name + '</option>');
                        });
                    },
                    error: function (ex) {
                        alert('Failed to retrieve Cities.' + ex);
                    }
                });
                return false;
            })
        });
    </script>

I'm thinking that it's in the Name ( data.Name ) that the page does not recognize me ... well that's what I call it (by default I always leave the initial with a capital letter, and ID is capitalized) but I do not know which opens wrongly done ....

Ariel, stacks with the following ... of promto you need in the UserController driver .. // DropDownList - > cascate // Cundinamarca - > (The Municipalities of this department, nothing more)

public JsonResult GetCities(int departmentId)
{
  db.Configuration.ProxyCreationEnabled = false;
  var cities = db.Cities.Where(c => c.DepartamentID == departmentId);
  return Json(cities);
}

and then in the view.

[Required(ErrorMessage = "The field {0} is required")]
    [MaxLength(50, ErrorMessage = "The field {0} must be maximum {1} Character length")]
    [Display(Name = "Name")]
    [Index("City_Name_Index", 2, IsUnique = true)]
    public string Name { get; set; }

and I keep getting the error:

  

The value 'undefined' is not valid for City.

and if I visualize, I have both departments (5) and cities (15) .... and there is a relationship between them, I have them in Database and I am working with Visual Studio 2015, sql server 2014, entity framework , I know that this part is irrelevant but to verify that I have everything right.

    
asked by Oscar Ortiz Pinzón 08.09.2016 в 20:05
source

0 answers