ASP.NET,Kendo UI,CS1660:无法将lambda表达式转换为'string'类型

evi*_*ise 6 c# asp.net-mvc lambda kendo-grid

解决方案在底部

我一直在努力通过Kendo.UI网格查看我的数据几天,猜测我无法理解如何做到这一点的基本概念,因为我是aspnet的新手以及所有这些东西.

Index.cshtml:

 @using Kendo.Mvc.UI
 @using System.Linq;


    @(Html.Kendo().Grid<CardsDemo.Models.CardsViewModel>()
    .Name("Grid")
    .Columns(columns =>
    {
    columns.Bound(p => p.Name).Title("Card ID").Width(130);
    columns.Bound(p => p.State).Title("State").Width(130);
    columns.Bound(p => p.ExpirationDate).Title("Expiration Date").Width(130);
})
.Pageable()
.Sortable()
.Scrollable(scr => scr.Height(430))
.Filterable()
.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .ServerOperation(false)  
    .Read(read => read.Action("GetCards", "Home"))   
 ).Render()
)
Run Code Online (Sandbox Code Playgroud)

HomeController.cs:

...
    [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

    [HttpPost]
        public JsonResult GetCards([DataSourceRequest] DataSourceRequest request)
        {
            var cards = repository.GetAll();
            var cardsvm = new CardsViewModel(cards);
            return Json(cardsvm.GetCards.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
...
Run Code Online (Sandbox Code Playgroud)

项目构建没有错误,但网页说:

Server Error in '/' Application.

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

    Compiler Error Message: CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type

    Source Error:


    Line 8:      .Columns(columns =>
    Line 9:      {
    Line 10:         columns.Bound(p => p.Name).Title("Card ID").Width(130);
    Line 11:         columns.Bound(p => p.State).Title("State").Width(130);
    Line 12:         columns.Bound(p => p.ExpirationDate).Title("Expiration Date").Width(130);

    Source File: c:\Users\me\Documents\Visual Studio 2013\Projects\CardsDemo\Views\Home\Index.cshtml    Line: 10 
Run Code Online (Sandbox Code Playgroud)

编辑:正如所建议的那样,我尝试放置断点并发现程序在索引结束后立即崩溃(包括homeControllers Index动作代码);

EDIT2:@clement layout.cshtml中的Kendo()用红色加下划线说

Error   3   'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'Kendo' and no extension method 'Kendo' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)   c:\Users\me\Documents\Visual Studio 2013\Projects\CardsDemo\Views\Home\Index.cshtml 3   12  CardsDemo
Run Code Online (Sandbox Code Playgroud)

我相信它是一个与Visual Studio相关的错误,它也连接到IntelliSense在cshtml文件中无法正常工作.我的同事说他们在他们的项目中也有这个,但是他们只是忽略它,而且它有效.

解决方案:如果您将Index.cshtml更改为:

@model ICollection<CardsDemo.Models.CardViewModel>

    @(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Name);
        columns.Bound(p => p.State);
        columns.Bound(p => p.ExpirationDate);
    })
    .Pageable()
    .Sortable()
    .Scrollable(scr => scr.Height(430))
    .Filterable()
    .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Read(read => read.Action("GetCards", "Home"))
                )
    )
Run Code Online (Sandbox Code Playgroud)

cle*_*ent 0

看起来您使用 Linq 时没有将System.Linq嵌入 到视图中。

编辑:您可以将断点放入视图/控制器中,并确保发送到视图的“Name”属性是一个字符串吗?