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; }
}
}
What is the type returned by getFilledReportWithEntitiesById()
?
Presumably, it's a Application.Database.ACCIDENT
. All the view engine does is invoke .ToString()
on what it's given. And the default implementation for .ToString()
on any reference type (any child of object
basically) is to return the type name.
If you want a custom string representation of your type, then that type needs to override .ToString()
. For example:
public override string ToString()
{
return string.Format("{0} - {1}", ID, Name);
}
If the object has an ID
property and a Name
property then its string representation would then be those values separated by a hyphen (with spaces in between). However you want to structure the string representation of your object would be done within this method.
Conversely, if you don't want it to be a string, but want the JavaScript code to use it as an object, then you want to serialize it to JSON. Something like this:
var data = @Json.Encode(ViewBag.Report);
(You might need to tweak that a little bit, I don't have an environment handy to test it. But you get the idea... To use it as an object in JavaScript code it needs to be serialized to a JavaScript object literal.)