I am having a issue where my select dropdowns are working for some and not others. I have debugger in the code and breakpoints. When I select the first list which is Company it drops down with all the available Companies. That then populates all the Addresses for that Company. Some work fine and then others do not.
It hits the controller JsonAction on the breakpoint I get a Read on the count which works fine and then does not populate. I am getting a Object error in the Console window and it says first that there is a 500 error that it cannot find the action, but it does because it hits the breakpoint. Then the object error seems to be reading the entire page html Under Response Text of the Object.
My Controller:
[HttpGet]
public ActionResult PlaceQuote()
{
GeneralEntities generalEntities = new GeneralEntities();
List<SelectListItem> Company = new List<SelectListItem>();
Quotes casModel = new Quotes();
List<CompanyNames> companies = generalEntities.CompanyNames.ToList();
companies.ForEach(x =>
{
Company.Add(new SelectListItem { Text = x.CompanyName, Value = x.CompanyId.ToString() });
});
casModel.CompanyNames = Company;
return View(casModel);
}
My Dropdowns:
<div class="form-group ">
@Html.LabelFor(model => model.CompanyId, htmlAttributes: new { @class = "control-label" })
@Html.DropDownListFor(model => Model.CompanyId, new SelectList(Model.CompanyNames, "Value", "Text"), "---Select Company---", new { @class = "form-control ", @id = "ddlCompany" })
@Html.ValidationMessageFor(model => model.Address.Country, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.AddressId, htmlAttributes: new { @class = "control-label " })
@Html.DropDownListFor(model => Model.AddressId, new List<SelectListItem>(), "---Select Address---", new { @class = "form-control ", @id = "ddlAddress" })
@Html.ValidationMessageFor(model => model.AddressId, "", new { @class = "text-danger" })
</div>
My JavaScript:
$(function () {
$('#ddlCompany').change(function () {
debugger;
$.ajax({
type: "post",
url: "/Users/AddAddress",
data: { companyId: $('#ddlCompany').val() },
datatype: "json",
traditional: true,
error: function (_, err) {
console.log(_, err)
},
success: function (data) {
debugger;
$('#ddlAddress')
.empty();
$.each(data, function (index, value) {
$('#ddlAddress').append('<option value="' + value.AddressId + '">' + value.Line2 + '</option>');
});
}
});
});
$('#ddlAddress').change(function () {
debugger;
$.ajax({
type: "post",
url: "/Users/AddUser",
data: { userId: $('#ddlAddress').val() },
datatype: "json",
traditional: true,
error: function (_, err) {
console.log(_, err)
},
success: function (data) {
$('#ddlUser')
.empty();
$.each(data, function (index, value) {
$('#ddlUser').append('<option value="' + value.FirstName + ' ' + value.LastName + '">' + value.FirstName + ' ' + value.LastName + '</option>');
});
}
});
});
});
My JsonAction:
public JsonResult AddAddress(string companyId)
{
int Id;
Id = Convert.ToInt32(companyId);
var address = (from a in db.Addresses where a.CompanyId == Id select a).ToList();
return Json(address);
}
I have checked my tables and there doesn't seem to be anything wrong with the data. The only difference in the records that do and do not populate, is that I added them through the website the ones that populate were added by Customers. The records that do not populate do not have users in the system, but my code checks for users in the next dropdown but I would think that the error would occur when selecting the user, Which is not working for any of them right now. When I remove the second part of the JavaScript to not select a user and just have user as a EditorFor, none of them work.
I am really confused on this and need some guidance.
UPDATE:
I thought I would mention that I have found that one of them that I added does work. I also Successfully removed the dropdown for users and the ones that populated before, still work. Which rules out errors with users and Customers I added.
Here is a screenshot of the Console window:
UPDATE: Still trying to debug this.. Below is what Json is bringing back on a good selection vs a bad. The bad seems to be the whole html page instead of a json string..
UPDATE: Trying all angles.. I am going straight to the url with an Id. The ones that work produce a string. The ones that do not produce a 500 Error. In Elmah it is saying - A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Addresses_AA62E15399B3645C48D42185AF1DAE6F4765400DBB7E67EFA5202BAE2D5CA8CE'.
I have looked it up and did add:
public JsonResult AddAddress(string companyId)
{
int Id;
Id = Convert.ToInt32(companyId);
db.Configuration.ProxyCreationEnabled = false;
var address = (from a in db.Addresses where a.CompanyId == Id select a).ToList();
return Json(address, JsonRequestBehavior.AllowGet);
}
The db.Configuration.ProxyCreationEnabled = false;
Seemed to cure the Issue. I am Unfamiliar as to why it was doing this on some and not all. Anyone that can explain that to me would be great...