Edward.K
Edward.K

Reputation: 566

how to return Json with a list from other classes?

I'm able to return a list from controller below to view:

public ActionResult FillSegment(int siteid)
{
    var segment = db.Segment.Where(c => c.SiteID == siteid).Select(x => new
    {
        SegmentID = x.SegmentID, 
        SegmentName = x.SegmentName
    });
    return Json(segment, JsonRequestBehavior.AllowGet);
}

But if i want to do the query operation in other classes, and i call the class from FillSegment Action, how it should return?

Controller:

public ActionResult FillSegment(int siteid)
{
    SegmentHelper segmenthelper = new SegmentHelper();      
    return Json(segmenthelper.FillSegment(siteid), JsonRequestBehavior.AllowGet);
}

segmenthelper Class:

public List<string> FillSegment(int siteid)
{
    using (DBConnection db = new DBConnection())
    {
        var segment = db.Segment.Where(c => c.SiteID == siteid).Select(x => new
        {
            SegmentID = x.SegmentID,
            SegmentName = x.SegmentName
        });
        return segment.ToList(); <-- Cannot convert generic list to generic list?
    }
}

Upvotes: 0

Views: 158

Answers (3)

Andriy Buday
Andriy Buday

Reputation: 1989

In such cases it is probably better to introduce a ViewModel class and return it. You might want to use some mapping library.

public class SegmentViewModel
{
    public string SegmentID { get; set; } //not sure if it of string type
    public string SegmentName { get; set; }
}

public IEnumerable<SegmentViewModel> FillSegment(int siteid)
{
    using (DBConnection db = new DBConnection())
    {
        return db.Segment.Where(c => c.SiteID == siteid).Select(x => new SegmentViewModel
        {
            SegmentID = x.SegmentID,
            SegmentName = x.SegmentName
        });
    }
}

// you can also use JsonResult instead of ActionResult
public JsonResult FillSegment(int siteid)
{
    SegmentHelper segmenthelper = new SegmentHelper();      
    return Json(segmenthelper.FillSegment(siteid), JsonRequestBehavior.AllowGet);
}

Upvotes: 1

Im not 100% sure but i think this should work

Controller:

     public ActionResult FillSegment(int siteid)
     {
        SegmentHelper segmenthelper = new SegmentHelper();      
        return Json(segmenthelper.FillSegment(siteid), JsonRequestBehavior.AllowGet);
     }

Segmenthelper Class:

    public list<Segment> FillSegment(int siteid)
    {
        using (DBConnection db = new DBConnection())
        {
            var segment = db.Segment.Where(c => c.SiteID == siteid).Select(x => new
        {
            SegmentID = x.SegmentID,
            SegmentName = x.SegmentName
        }).toList();
        return segment;
        }
    }

Upvotes: 0

Arkhangelskiy Evgeniy
Arkhangelskiy Evgeniy

Reputation: 622

The Select statement will return you a sequence of anonymous objects with properties SegmentID and SegmentName and they cannot be converted to list of strings. You had to change return type of FillSegment.

Upvotes: 0

Related Questions