Kode terlihat sebagai berikut:

1) Kelas entitas:

public partial class EnergeticalComplex : TrackableBaseObject, IWithDbAffiliateFieldBlockField
{
    private ICollection<Cell> _cells;
    private ICollection<Document> _documents;

    public int? AffiliateId { get; set; }
    public virtual Affiliate Affiliate { get; set; }

    public int? FieldBlockId { get; set; }
    [AutoQueryRoot]
    public virtual FieldBlock FieldBlock { get; set; }

    public int? FieldId { get; set; }
    [AutoQueryRoot]
    public virtual Field Field { get; set; }

    [NotNull]
    [AutoQueryRoot]
    public virtual ICollection<Cell> Cells
    {
        get { return _cells ?? (_cells = new List<Cell>().WithLock(SyncRoot)); }
    }

    [NotNull]
    [TrackableRelation]
    public virtual ICollection<Document> Documents
    {
        get { return _documents ?? (_documents = new List<Document>().WithLock(SyncRoot)); }
    }
}

2) Di pengontrol:

public JsonResult ComboBoxBalanse(int fieldId)
{
    List<EnergeticalComplex> eComplex;
    using (var c = _contextFactory.CreateContext())
    {
        var eComplexesSet = c.Set(c.GetType()
            .EntityTypes()
            .First(t => typeof(IEnergeticalComplex).IsAssignableFrom(t)));
        eComplex = eComplexesSet
            .Cast<EnergeticalComplex>()
            .Where(ec => ec.Field.Id == fieldId && !ec.Name.Contains("KTP"))
            .ToList();
    }

    ViewData["EnergeticalComplexList"] = eComplex;
    ViewData["fieldId"] = fieldId;

    return Json(eComplex, JsonRequestBehavior.AllowGet);
}

3) Dalam modul JavaScript:

function onClickShowButton() {
    var selectBoxField = $("#divFieldsComboBox").dxSelectBox("instance");
    var indField = selectBoxField.option("value");

    $.ajax({
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        url: '/LostEnergyCalculation/ComboBoxBalanse',
        dataType: 'json',
        data: "{ fieldId: " + indField + " }"
    }).done(function (data) {
        alert(JSON.stringify(data));
    });
}

Alur program tidak masuk ke blok setelah .done().

1
tesicg 10 November 2017, 12:02

1 menjawab

Jawaban Terbaik

Sepertinya Anda menggunakan kelas entitas yang memiliki variabel anggota kelas lebih lanjut! Dalam hal ini ada kemungkinan masalah serialisasi Json karena referensi melingkar di objek 'eComplex' Anda. Untuk mencegahnya, deklarasikan kelas baru tanpa semua variabel anggota sub-kelas atau gunakan .Select() untuk hanya mengambil elemen yang diperlukan. yaitu Di Pengontrol Anda

eComplex = eComplexesSet
            .Cast<EnergeticalComplex>()
            .Where(ec => ec.Field.Id == fieldId && !ec.Name.Contains("KTP"))
            .Select(m=> new {
             VariableName = m.ModelVariableName,
             // Same for elements which you needed for 
             });
1
Ismail Asghar 10 November 2017, 10:31