Sergej Popov
Sergej Popov

Reputation: 3021

Binding Telerik chart in MVC cs1660 error

I'm trying to bind Telerik Chart to IEnumerablge<MyModel> Inside Partial View

My Model

public class MyModel
{
    private string identifier;
    private DateTime date;
    private int visits;
}

Partial View

@model IEnumerable<MyModel> 
@{
Html.Telerik().Chart(Model)
    .Name("Visits")
    .Legend(legend => legend.Visible(true).Position(ChartLegendPosition.Bottom))
    .Series(series => {
        series.Line("CurrentMonth").Name("Current Month")
              .Markers(markers => markers.Type(ChartMarkerShape.Triangle));
        series.Line("PrevMonth").Name("Previous Month")
              .Markers(markers => markers.Type(ChartMarkerShape.Square));
    })
    .CategoryAxis(axis => axis.Categories(s => s.date))
    .ValueAxis(axis=>axis.Numeric().Labels(labels=> labels.Format("{0:#,##0}")))
    .Tooltip(tooltip => tooltip.Visible(true).Format("${0:#,##0}"))
    .HtmlAttributes(new { style = "width: 600px; height: 400px;" });
}

Getting Following error:

CS1660: Cannot convert lambda expression to type 'System.Collections.IEnumerable' because it is not a delegate type

On Following line of code:

.CategoryAxis(axis => axis.Categories(s => s.date))

Thank you!

Upvotes: 0

Views: 927

Answers (1)

Sergej Popov
Sergej Popov

Reputation: 3021

There were multiple issues with the code. after changing bits in pieces in code I finally figured out following works fine:

@{    
Html.Telerik().Chart(Model)
    .Name("SampleChart")
    .Tooltip(tooltip => tooltip.Visible(true).Format("${0:#,##0}"))
    .Legend(legend => legend.Position(ChartLegendPosition.Bottom))
    .Series(series =>
    {
        series.Line(s => s.visits).Name("Visits");
        series.Line(s => s.hits).Name("Hits");
    })
    .CategoryAxis(axis => axis
        .Categories(s => s.date)
    )
    .Render();
}

So there was Render() missing in the end.

Also paramter in series.Line("CurrentMonth") must match the field name in the myObject, Or field selected through lambda expression.

Upvotes: 1

Related Questions