How to get a complex object from a controller for viewing through an Ajax request?

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?

+4
source share
3 answers

Do I need to do something to transfer complex objects from the controller for viewing?

Yes, you should use view models. You cannot JSON serialize hierarchies of objects that link circular references to each other. You will need to break these circular links if you want JSON to serialize your model and send it by cable. The JSON serialization format simply does not support this.

+4
source

The default JSON parser is not capable of handling circular references. You probably have a Company with Emails property, and each Email again has a link to the company.

You will need to submit a view model, as Darin suggested (which is best anyway):

http://blog.davebouwman.com/2011/12/08/handling-circular-references-asp-net-mvc-json-serialization/

... or you can replace the default JSON serializer with something like Json.NET , which can handle circular references:

Cyclic link exclusion when serializing LINQ to SQL classes

+1
source

You can simply make ajax post call using dataType = json

  $.ajax({ type: 'POST', url: '@Url.Action("actionName", "controllerName")', data: JSON.stringify({ *Perameter If Any* }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { if (result != null) { } else { } } }); 

And your action method should be the result of Json

 public JsonResult ActionName(perameter) { var modle= getobject(); return Json(modle); } 

In the case value above, you will end up with an ajax call method that will result in the same object that you sent ... No need to serialize or any tricks. Simple as it looks

0
source

All Articles