0

I get an error ("Cannot convert lambda expression to type 'string' because it is not a delegate type") during converting the lambda expression in controller. I have 3 entities in as below:

Entities:

public class Student
{
    public int ID { get; set; }

    public string Course { get; set; }  

    public int CityID { get; set; }

    public virtual City City { get; set; }
}


public class City
{
    public int ID { get; set; }        

    public string Name { get; set; }

    public int RegionID { get; set; }

    public virtual Region Region { get; set; }

    public virtual ICollection<Student> Students { get; set; }    
}


public class Region
{
    public int ID { get; set; }

    public string Name { get; set; }

    public virtual ICollection<City> Cities { get; set; } 
}


Controller:

public ActionResult Index_Read([DataSourceRequest] DataSourceRequest request)
    {
        var dataContext = repository.Students;
        var students = dataContext.ToDataSourceResult(request, m => new 
        {
            ID = m.ID,
            Course = m.Course,

            City = m.City.Name, //I can get City name and show it in View.
            MyRegionName = m.City.Region.Name //I can get region name and assign it to 
//"MyRegionName" parameter in JSON. However in View I cannot get it using "MyRegionName" paremeter  
        });           

        return Json(students, JsonRequestBehavior.AllowGet);
    }


View:

@model IEnumerable<Student>


@(Html.Kendo().Grid<Student>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(m => m.ID);
        columns.Bound(m => m.Course);
        columns.Bound(m => m.City);
        columns.Bound(m => m.MyRegionName);
    })
    .Pageable()
    .Sortable()
    .Filterable()
    .Scrollable()
    .Groupable()    
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Index_Read", "Student"))
    )        
)

Here is the point that may cause the problem in the Controller and View:

City = m.City.Name, //I can get City name and show it in View.
MyRegionName = m.City.Region.Name //I can get region name and assign it to the  "MyRegionName" parameter in JSON. However in View I cannot get it using "MyRegionName" paremeter.

May it be related to that there is City parameter in the Student entity. But there is no MyRegionName property in the City entity.

1 Answer 1

2

I assume this happens because there is no property called MyRegionName for the Student class.

You have two options

1) Create a ViewModel that looks like your Student model but make it has such property. Also make sure inside the projection function of the ToDataSourceResult you create those new ViewModel types instead of anonymous object.

2) Just use a Template column. e.g.

columns.Template(@<text></text>).Title("MyRegionName").ClientTemplate("#=MyRegionName#");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your explanation. I would like to use the 2nd option as it is really very simple. On the other hand, there is a little problem: The column header does not seem to be the other column headers. I tried to find the class the others and use on this header, but it did not work (using "k-link" class). Any idea that I can apply standard kendo ui grid header class to this one? I tried to use header attributes as below, but not worked. Many thanks again.
columns.Template(@<text></text>).Title("MyRegionName").ClientTemplate("#=MyRegionName#").HeaderHtmlAttributes(new { @class = "k-link" });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.