[RESOLVED]ViewBag value always pass as namespace string

I m trying to pass a object to view with viewbag and pass it to javascript parameter in the view But when comes to assign object to script value it looks like string likes its namespace

in controller:

public ACCIDENT_REPORTS getFilledReportWithEntitiesById(int accidentReport_id)
        {
            ACCIDENT_REPORTS report = new ACCIDENT_REPORTS();
            report = context.ACCIDENT_REPORTS.Include("ACCR_ENTITIES").Where(a => a.ID == accidentReport_id).FirstOrDefault();
            return report;
        }

    ViewBag.Report = DatabaseContext.Current.AccidentReportingRepository.getFilledReportWithEntitiesById(id); //its okey, all data in viewbag

in view:

<script>
    debugger
    var data = '@ViewBag.Report';
</script>
// in debugger value looks like; var data = 'Application.Database.ACCIDENT_REPORTS;

Why it looks like string ? how can I pass contents of viewbag to javascript value

here is my entity object return type:

public partial class ACCIDENT_REPORTS
    {
        public ACCIDENT_REPORTS()
        {
            this.ACCR_ENTITIES = new HashSet<ACCR_ENTITIES>();
        }

        public decimal ID { get; set; }
        public decimal FACILITY_ID { get; set; }
        public Nullable<System.DateTime> START_DATE { get; set; }
        public Nullable<System.DateTime> END_DATE { get; set; }
        public string TITLE { get; set; }
        public Nullable<decimal> ACCIDENT_TYPE { get; set; }
        public Nullable<decimal> REPORTED_UNDER { get; set; }
        public Nullable<decimal> SEVESOII_STATUS { get; set; }
        public Nullable<decimal> INDUSTRIAL_ACTIVITY { get; set; }
        public string REASON_FOR_REPORTING { get; set; }
        public string ACCIDENT_DESCRIPTION { get; set; }
        public string SITE_DESCRIPTION { get; set; }
        public string UNIT_DESCRIPTION { get; set; }
        public string CAUSES_OF_ACCIDENT { get; set; }
        public string CONSEQUENCES { get; set; }
        public string EMERGENCY_RESPONSE { get; set; }
        public string LESSONS_LEARNED { get; set; }
        public string ACCIDENTS_INVOLVING { get; set; }
        public Nullable<decimal> REPORT_STATUS { get; set; }

        public virtual ICollection<ACCR_ENTITIES> ACCR_ENTITIES { get; set; }
    }
}

Viewbag.Report is (I assume) a collection of complex data.  Exactly what are you expecting it to look like as a string?  .net doesn’t know either, so when you ask an object to represent itself as a string and you haven’t told it how it should look as a string
then it simply returns its type as a name which is what you’re seeing.  If you want the object data in JSON format then use

var data = @Html.Raw(Json.Encode(ViewBag.Report))

Something like that anyway.

When I try json encode it throws error like ""A circular reference was detected while serializing an object of type…" 
Why it is happen ?

Yener

// in debugger value looks like; var data = ‘Application.Database.ACCIDENT_REPORTS;

That’s because ViewBag.Report represents your entire ACCIDENT_REPORTS class.  If you want to get specific properties of your model, you need to explicitly request them:

<script>
  var reportID = '@ViewBag.Report.ID';
  var reportTitle = '@ViewBag.Report.TITLE';

  // etc...
</script>

Yener

When I try json encode it throws error like ""A circular reference was detected while serializing an object of type…" 
Why it is happen ?

Quite a common problem with complex objects.  Not everything is serialisable.  You will probably have to create a "view model" which is a new class that has just the items you need to display, using simple types (no hashsets, even datetime is iffy), avoid
collections that reference the same parent type etc.  Without knowing the exact data you’re using it’s hard to give specific advise.

This is what I need to do. Thank you

Leave a Reply