Are there any issues with using anonymous types with the Json() method in asp.net mvc?
I recently had an issue where returning an anonymous type from a Linq .First() would not work when returned to the jQuery caller. I created a simply subclass inside my controller and simple assigned values from the query object and that seemed to serialize and work fine.
This worked:
public class JsonJob
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime DateAdded { get; set; }
public DateTime? DateModified { get; set; }
};
var jsonJob = from job in db.Jobs
where job.Id == jobId
select new JsonJob
{
Id = job.Id,
Title = job.Title,
Description = job.Description,
DateAdded = job.DateAdded,
DateModified = job.DateModified
};
return Json(jsonJob.First());
But this did not:
var jsonJob = from job in db.Jobs
where job.Id == jobId
select job;
return Json(jsonJob.First());