【问题标题】:Implementation of Kendo Listview control in MVCMVC中Kendo Listview控件的实现
【发布时间】:2014-09-24 06:22:06
【问题描述】:

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

“CS1977:不能使用 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);
        }

【问题讨论】:

  • 您是否将模型传递给此视图?您应该添加:“@model K_SampleProject.Models.CourseModel”并将模型传递给此视图,除非您从父视图隐式执行此操作。您传递的模型是否具有正确的类型(K_SampleProject.Models.CourseModel)?
  • 当我尝试将模型传递给视图页面时,它给出了编译错误 The best overloaded method match for 'Kendo.Mvc.UI.Fluent.WidgetFactory.ListView(System .Collections.Generic.IEnumerable)' 有一些无效参数
  • 我也用模型和控制器代码更新了我的问题。请看一下。
  • 在你的对象模型中是否有dynamic 类型的项目潜伏在任何地方?
  • 没有。我又查了一遍,没有。

标签: asp.net-mvc-4 razor kendo-ui kendo-asp.net-mvc kendo-listview


【解决方案1】:

您的代码中的主要错误是您将单个 CourseModel 类传递给列表,而它需要 CourseModel 列表。 因此,您的 Controller 应如下所示:

    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&lt;CourseModel&gt;
  • 如果是 PartialView(不是索引之类的主视图),则更改返回:return PartialView(result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多