ASP.NET MVC Telerkik AutoComplete不显示记录

时间:2015-01-26 07:29:36

标签: asp.net-mvc autocomplete telerik-mvc

我希望Telerik MVC UI AutoComplete与我的EducationEntities一起使用,但返回null。不知道为什么? 这是我的代码:

@(Html.Kendo().AutoComplete()
      .Name("students")
      .DataTextField("DisplayName")
      .Placeholder("Enter student name")
      .DataSource(source =>
      {
        source.Read(read =>
        {
            read.Action("GetStudents", "StudentDetails").Data("onAdditionalData"); ;
        }).ServerFiltering(true);
      })          
      .HtmlAttributes(new { style = "width:240px" })
      .Filter(FilterType.Contains)
      .MinLength(1)
      .Suggest(true)
      .Height(340)
      .HeaderTemplate("<div class=\"dropdown-header\">" +
                    "<span class=\"k-widget k-header\">Photo</span>" +
                    "<span class=\"k-widget k-header\">Contact info</span>" +
                "</div>")
      .Template("<span><img src='/Content/Images/${data}.jpg' " +
                        "width='20' height='20' />&nbsp;${data}</span>")
      .Template("<span class=\"k-state-default\"><h3>#: data.DisplayName #</h3></span>")
      .Template("<span class=\"k-state-default\"><img src=\"" + Url.Content("~/Content/Images/") + "#:data.StudentID#.jpg\" alt=\"#:data.StudentID#\" /></span>" +
                "<span class=\"k-state-default\"><h3>#: data.DisplayName #</h3><p>#: data.Address #</p></span>")
)

这是我的行动,应该返回完整的学生详细信息。

public JsonResult GetStudents(string name)
    {
        var students = new EducationEntities().Student.Select(student => new Student
        {
            StudentID = student.StudentID,
            DisplayName = student.DisplayName,
            FirstName = student.FirstName,
            LastName = student.LastName                
        });

        if (!string.IsNullOrEmpty(name))
        {
            students = students.Where(p => p.DisplayName.Contains(name));
        }

        return Json(students, JsonRequestBehavior.AllowGet);
    }

1 个答案:

答案 0 :(得分:0)

您应该将操作更改为:

创建学生模型

 public class StudentModel
 {
      public int StudentID { get; set; }
      public string DisplayName { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
 }

并相应地更改操作

public JsonResult GetStudents(string name)
{
     //initialize the result
     var students = new List<StudentModel>();

     //check for filter value; if any, than filter
     if (!string.IsNullOrEmpty(name))
     {
         students = new EducationEntities().Student
                    .Where(student => student.DisplayName.Contains(name))
                    .Select(student => new StudentModel
                    {
                        StudentID = student.StudentID,
                        DisplayName = student.DisplayName,
                        FirstName = student.FirstName,
                        LastName = student.LastName                
                    }).ToList();
     }
     else
     {
         //depending on your requirements, you could return all the students when there's no filter set
         students = new EducationEntities().Student.Select(student => new StudentModel
                    {
                        StudentID = student.StudentID,
                        DisplayName = student.DisplayName,
                        FirstName = student.FirstName,
                        LastName = student.LastName                
                    }).ToList();
     }

     return Json(students, JsonRequestBehavior.AllowGet);
}

请注意,您需要明确调用ToList()才能执行查询,并使用结果填充变量。