1
    public IList<Event> SearchEvents(DateTime fromDate, DateTime toDate, int categoryId, string eventName )
    {

        var  query = context.Events.Include("City").Where(e => e.EventCategoryID == (categoryId <= 0 ? e.EventCategoryID : categoryId)
            && (e.StartDate.Value.Month >= fromDate.Month)
            && (e.EndDate.Value.Month <= toDate.Month)
            && ( e.StartDate.Value.Day>= fromDate.Day) 
            && (e.EndDate.Value.Day <= toDate.Day )
            && (e.StartDate.Value.Year >= fromDate.Year)
            && (e.EndDate.Value.Year <= toDate.Year)
            && string.IsNullOrEmpty(eventName)?  eventName.Contains(e.EventName):   eventName.Contains(eventName));

      return query.ToList();
    }

 public JsonResult SearchEvents(string from,string to,int categoryId, string eventName)
 {
        DateTime frmDate= Convert.ToDateTime(from);
        DateTime toDate = Convert.ToDateTime(to);
        var list = _eventRepository.SearchEvents(frmDate,toDate,categoryId,eventName);  
        return Json(list, JsonRequestBehavior.AllowGet);
 }

i getting Error like:

Error :' A circular reference was detected while serializing an object of type 'CGWeb.Models.Repositories.Models.Event'.

how can solve this issue without removing virtual keyword?.please share..!

//

@Marc Gravell This is my Model

 [Table("Table_Events")]
public partial class Event
{
    [Key]
    public int ID { get; set; }

    //public int? LocationId { get; set; }
    //public int? ImageId { get; set; }
    public string EventName { get; set; }

    [NotMapped]
    public string EventAddress { get; set; }

    public string EventUrl { get; set; }
    public string EventDesc { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public Nullable<System.DateTime> EndDate { get; set; }

    public Nullable<int> EventCategoryID { get; set; }
    public int CityID { get; set; }
    public int Viewed { get; set; }

    [ForeignKey("EventCategoryID")]
    public virtual EventCategory EventCategory { get; set; }
    //[ForeignKey("ImageId")]
    [NotMapped]
    public virtual ImageViewModel Image { get; set; }

    //[ForeignKey("LocationId")]
    //public virtual Location Location { get; set; }


    [ForeignKey("CityID")]
    public virtual City City { get; set; }

    [NotMapped]
    public bool ISSponsorship { get; set; }

    [NotMapped]
    public Organizer Organizer { get; set; }

    //[NotMapped]
    [ForeignKey("EventId")]
    public virtual IList<Attending> Attending { get; set; }
}
1
  • 1
    Are you sure about that date comparison? 12th of june would not be before 1st of july ... Commented Sep 2, 2011 at 10:37

1 Answer 1

3

This is nothing to do with the virtual keyword; it relates to the object graph. We can't see your graph, but the classic scenario here is a parent/child bidirectional relationship, i.e. where the parent has a .Children and the child has a .Parent.

A tree-serializer (such as xml, json, etc) will usually walk any members that are not explicitly marked to be ignored. Hence you would get an infinite loop as it went around that circle forever. Options:

  • use a non-cyclic DTO at this boundary (that is what I would do)
  • mark the offending back-reference for exclusion (the mechanism for this varies per serializer)
Sign up to request clarification or add additional context in comments.

3 Comments

sir, Can you tell little bit about these two Options? .how actually i have to solve this?
@Justinonday without seeing your object model, not really. With JavaScriptSerializer you would normally use [ScriptIgnore] to tell it to ignore it, but if your model is generated that may not be possible - in which case, I would simply not use the EF model for the last step, but project the data instead into a simple flat class model that represents exactly what you want the json to look like
sir i put my Model please review it..!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.