繁体   English   中英

Kendo Listview控件在MVC中的实现

[英]Implementation of Kendo Listview control in MVC

我试图为MVC实现Kendo UI的Listview控件。 我试图将列表视图与我的模型绑定,但出现此错误:

“ CS1977:如果不先将lambda表达式强制转换为委托或表达式树类型,就不能将lambda表达式用作动态调度的操作的参数”

我用相同的错误检查了关于stackoverflow的其他问题,但我不知道此错误的原因,因为这是kendo语法,据我所知我的代码没有任何错误。

错误在此行中:: .DataSource(ds => ds

查看页面:

@{
    ViewBag.Title = "Courses";
}
@using Kendo.Mvc.UI

<h2>Courses</h2>

<a href="/Home/Registration">Back</a>

<div class="bodywrap">
    <div class="CommonClass">

        @( Html.Kendo().ListView<K_SampleProject.Models.CourseModel>(Model)
          .Name("listView")
          .TagName("div")
          .ClientTemplateId("template")
          .DataSource(ds => ds
          .Model(model =>
          {
              //The unique identifier (primary key) of the model is the ProductID property
              model.Id(p => p.ProductID);

              // Declare a model field and optionally specify its default value (used when a new model instance is created)
              model.Field(p => p.ProductName).DefaultValue("N/A");

              // Declare a model field and make it readonly
              model.Field(p => p.UnitPrice).Editable(false);
          })
)
    .Pageable()
         )
    </div>
</div>


<script type="text/x-kendo-tmpl" id="template">
    <div class="product">
        <img src="@Url.Content("~/content/web/foods/")${ProductID}.jpg" alt="${ProductName} image" />
        <h3>${ProductName}</h3>
        <dl>
            <dt>Price:</dt>
            <dd>${kendo.toString(UnitPrice, "c")}</dd>
        </dl>
    </div>
</script>

Model
namespace K_SampleProject.Models
{
    public class CourseModel
    {
        public List<tbl_Courses> CourseList { get; set; }
        public string ProductID { get; set; }
        public string ProductName { get; set; }
        public string UnitPrice { get; set; }
    }
}

Controller
  public ActionResult Courses()
        {
            CourseModel Model = new CourseModel();
            RegistrationService ObjService = new RegistrationService();
            Model.CourseList = ObjService.GetCourses();
            return View(Model);
        }

代码中的主要错误是,当期望使用CourseModel的列表时,您将单个CourseModel类传递给了列表。 因此,您的控制器应如下所示:

    public ActionResult Courses()
    {
        List<CourseModel> result;
        CourseModel Model = new CourseModel();
        RegistrationService ObjService = new RegistrationService();
        Model.CourseList = ObjService.GetCourses();
        result.Add(Model);
        return View(result);
    }

我还建议:

  • 在视图顶部添加@model List<CourseModel>
  • 如果它是PartialView(不是索引的主视图),则更改return为: return PartialView(result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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