为什么Entity Framework 6中的模型无法序列化数据?

时间:2016-03-12 08:05:43

标签: entity-framework model-view-controller serialization

我尝试从模型中获取数据并使用json格式传递给客户端。我写了这段代码但是在执行代码时,我收到内部服务器错误。调试代码时,会显示数据,例如此图像。

enter image description here

请建议。

[HttpPost]
public ActionResult GetCity(int idCountry)
{
    TravelEnterAdminTemplate.Models.LG.MyJsonResult myresult = new Models.LG.MyJsonResult();

    try
    {
        List<City> citylist = new List<City>();
        var citystable = db.Cities.Where(p => p.CountryId == idCountry).ToList();

        if (citystable != null)
        {
            foreach (var city in citystable)
            {
                myresult.obj = citystable;     
            }
            myresult.Result = true;
        }
        else
        {
            myresult.Result = false;
            myresult.message = "داده ای یافت نشد";
        }
    }
    catch (Exception e)
    {
        errorlog.Error("DeleteShopping", "157", e.Source.ToString(), e.Message);
        myresult.Result = false;
        myresult.message = "خطا در بارگذاری اطلاعات";
    }

    return Json(myresult, JsonRequestBehavior.AllowGet);
}

城市实体:

namespace TravelEnterAdminTemplate.Models.dbModel

{     使用系统;     使用System.Collections.Generic;

public partial class City
{
    public City()
    {
        this.ArtPlaces = new HashSet<ArtPlace>();
        this.Historicals = new HashSet<Historical>();
        this.Hospitals = new HashSet<Hospital>();
        this.Hotels = new HashSet<Hotel>();
        this.PhotoTables = new HashSet<PhotoTable>();
        this.PhotoTables1 = new HashSet<PhotoTable>();
        this.Restaurants = new HashSet<Restaurant>();
        this.Shoppings = new HashSet<Shopping>();
    }

    public int Id { get; set; }
    public string NameFA { get; set; }
    public string NameEN { get; set; }
    public int CountryId { get; set; }

    public virtual ICollection<ArtPlace> ArtPlaces { get; set; }
    public virtual Country Country { get; set; }
    public virtual ICollection<Historical> Historicals { get; set; }
    public virtual ICollection<Hospital> Hospitals { get; set; }
    public virtual ICollection<Hotel> Hotels { get; set; }
    public virtual ICollection<PhotoTable> PhotoTables { get; set; }
    public virtual ICollection<PhotoTable> PhotoTables1 { get; set; }
    public virtual ICollection<Restaurant> Restaurants { get; set; }
    public virtual ICollection<Shopping> Shoppings { get; set; }
}

}

0 个答案:

没有答案