简体   繁体   中英

I'm new to asp.net mvc and entity Jquery ajax error says 'The entity or complex type 'myModel'' cannot be constructed in a LINQ to Entities query.'

Here's my code on the controller any help would be helpful guys thank you very much!

public JsonResult GetJobList()
        {
            jQueryAjaxEntities db = new jQueryAjaxEntities();
            List<jobdetail> job = db.jobdetails.Select(x => new jobdetail
            {
                JobID = x.JobID,
                Task_Name = x.Task_Name,
                Description = x.Description,
                Date_Started = x.Date_Started,
                Date_Finished = x.Date_Finished,
                Status = x.Status,
            }).ToList();

            return Json(job, JsonRequestBehavior.AllowGet);
        }

I guess the issue is using the mapped entity itself. Try using a DTO (create new dto class)

public class JobDetailsDTO
{
     public int JobID { get; set; }
    // Other fields you may need
}

And then:

using(var db = new jQueryAjaxEntities())
{
    var job = db.jobdetails.Select(x => new JobDetailsDTO
        {
            JobID = x.JobID,
            Task_Name = x.Task_Name,
            Description = x.Description,
            Date_Started = x.Date_Started,
            Date_Finished = x.Date_Finished,
            Status = x.Status,
        }).ToList();

        return Json(job, JsonRequestBehavior.AllowGet);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM