I am trying to get a complex ViewModel for my view through jQuery $.getJSON . However, although it works for simple objects, when my view model contains other lists of objects as part of this, my Ajax request stops working.
This is how I retrieve the data,
$.getJSON('/Company/GetCompanies', function(data) { viewModel.model = ko.mapping.fromJS(data) ko.applyBindings(viewModel) });
This is a working model,
public class CompanyIndex { public IList<CompanyWithDetail> Companies { get; set; } public void FillCompanies() { UnitOfWork unitOfWork = new UnitOfWork(); unitOfWork.CompanyRepository.SetProxy(false); var CompanyFromDB = unitOfWork.CompanyRepository.GetCompanyWithDetails(); Companies = new List<CompanyWithDetail>(); foreach (Company company in CompanyFromDB) { CompanyWithDetail newCompany = new CompanyWithDetail(); newCompany.CompanyName = company.CompanyName; Companies.Add(newCompany); } unitOfWork.Dispose(); } }
This is the CompanWithDetail class,
// For sake of demonstration it only contains name public class CompanyWithDetail { public string CompanyName { get; set; } }
It works great. However, when I add
public IList<CompanyFaxNumber> FaxNumber { get; set; }
this property to the CompanyWithDetail class and populate it in the FillCompanies() method of the CompanyIndex viewmodel, my get ajax request will stop working.
This is my btw controller, in both cases it returns the correct data, but jquery $.getJSON does not receive when I add complex objects.
public ActionResult GetCompanies() { var model = new CompanyIndex(); model.FillCompanies(); return Json(model ,JsonRequestBehavior.AllowGet); }
EDIT 1:
By saying that getJSON is not receiving data, I mean that the body of the function is not executing.
$.getJSON('/Company/GetCompanies', function(data) { alert('test') });
For example, a warning works when there is no complex object, but it stops working when I add objects to the viewmodel.
EDIT 2
This is a mistake when I call "Company / GetCompanies" from the browser instead of ajax.
A circular link was found while serializing an object of type "CompanyManagement.Models.CompanyEmail".
Do I need to do something to transfer complex objects from the controller for viewing? Any ideas?