无法在MVC中绑定Kendo ListView
本文关键字:绑定 Kendo ListView MVC | 更新日期: 2023-09-27 18:24:58
我试图在MVC 4.0项目中绑定Kendo Listview,但无法绑定Listview。没有错误,但是listviewdiv显示为空。我不知道哪里出了问题。以下是我的代码:
查看页面
@{
ViewBag.Title = "Courses";
}
@using Kendo.Mvc.UI
@model K_SampleProject.Models.CourseModel
<h2>Courses</h2>
<div class="bodywrap">
<div class="CommonClass" style="padding-bottom: 3%;"><a href="/Home/Registration">Back</a></div>
<div class="CommonClass">
@( Html.Kendo().ListView(Model.CourseList)
.Name("listView")
.TagName("div")
.ClientTemplateId("template")
.DataSource(datasource => datasource
.Model(model =>
{
//The unique identifier (primary key) of the model is the ProductID property
model.Id(p => p.pkCourseId);
// Declare a model field and optionally specify its default value (used when a new model instance is created)
model.Field(p => p.CourseName).DefaultValue("N/A");
// Declare a model field and make it readonly
model.Field(p => p.Fee).Editable(false);
})
)
.Pageable()
)
</div>
</div>
<script type="text/x-kendo-tmpl" id="template">
<div class="product">
<h3>#:CourseName#</h3>
<p>#:kendo.toString(Fee, "c")#</p>
</div>
</script>
型号
public class CourseModel
{
public List<CourseListModel> CourseList { get; set; }
public int? CourseID { get; set; }
public string CourseName { get; set; }
public int? FEE { get; set; }
}
以下是我的注册服务:
public class RegistrationService
{
public List<CourseListModel> GetCourseDetail()
{
using (testEntities objContext = new testEntities())
{
return objContext.tbl_Courses.Select(p => new CourseListModel
{
CourseName = p.CourseName,
Fee = p.Fee,
pkCourseId = p.pkCourseId
}).ToList();
}
}
}
控制器
public ActionResult Courses()
{
RegistrationService ObjService = new RegistrationService();
CourseModel Model = new CourseModel();
Model.CourseList = ObjService.GetCourseDetail();
return View(Model);
}
我已经将列表视图与Model.CouseList绑定,当页面加载时会填充该列表。那么,是否也有必要在数据源中给出READ事件,如kendo ListView演示和该链接的文档中所示。
我完全搞不懂它有约束力。我也检查了模板,但它与剑道文档
Kendo ListView需要返回一个DataSourceResult。因此,在listView中的数据源中添加一个Read(),并让它调用以下操作:
public ActionResult GetCourses([DataSourceRequest] DataSourceRequest request)
{
RegistrationService ObjService = new RegistrationService();
return ObjService.GetCourseDetail().ToDataSourceResult(request);
}