无法将项目加载到Kendo()。MultiSelect(Unable to load items into Kendo().MultiSelect)

编程入门 行业动态 更新时间:2024-10-27 13:30:07
无法将项目加载到Kendo()。MultiSelect(Unable to load items into Kendo().MultiSelect)

我正在使用代码优先方法开发一个带有EF脚手架的ASP.net MVC应用程序。 使用Kendo MultiSelect的视图之一无法加载项目。 在运行时,MultiSelect只显示文字“未定义”。

这是模型

public class SessionStudent { public int SessionStudentID { get; set; } public int SessionID { get; set; } [Display(Name = "Session")] public virtual Session Session { get; set; } public IEnumerable<int> SelectedStudentIDs { get; set; } public IEnumerable<Student> Student { get; set; } public int GradeID { get; set; } [Display(Name = "Grade")] public virtual Grade Grade { get; set; } }

这里是Controller中的Get和Post方法

// GET: SessionStudents/Create public ActionResult Create() { ViewBag.GradeID = new SelectList(db.Grades, "GradeID", "GradeName"); ViewBag.SessionID = new SelectList(db.Sessions, "SessionID", "SessionName"); ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FName"); return View(); } // POST: SessionStudents/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "SessionStudentID,SessionID,GradeID")] SessionStudent sessionStudent) { if (ModelState.IsValid) { db.SessionStudents.Add(sessionStudent); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.GradeID = new SelectList(db.Grades, "GradeID", "GradeName", sessionStudent.GradeID); ViewBag.SessionID = new SelectList(db.Sessions, "SessionID", "SessionName", sessionStudent.SessionID); return View(sessionStudent); }

这里是视图中的MultiSelect

<div class="form-group"> @Html.LabelFor(model => model.SessionStudentID, "StudentID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.Kendo().MultiSelectFor(model => model.SessionStudentID).BindTo((SelectList)ViewBag.StudentID).DataTextField("FName").DataValueField("StudentID").Name("SelectedStudentIDs") @Html.ValidationMessageFor(model => model.SessionStudentID, "", new { @class = "text-danger" }) </div> </div>

任何在这方面的帮助,高度赞赏。

I am developing an ASP.net MVC application with EF Scaffolding using the code first approach. One of the view using Kendo MultiSelect is unable to load the items. At run time, the MultiSelect only displays the text "undefined".

Here is the Model

public class SessionStudent { public int SessionStudentID { get; set; } public int SessionID { get; set; } [Display(Name = "Session")] public virtual Session Session { get; set; } public IEnumerable<int> SelectedStudentIDs { get; set; } public IEnumerable<Student> Student { get; set; } public int GradeID { get; set; } [Display(Name = "Grade")] public virtual Grade Grade { get; set; } }

Here is the Get and Post methods in Controller

// GET: SessionStudents/Create public ActionResult Create() { ViewBag.GradeID = new SelectList(db.Grades, "GradeID", "GradeName"); ViewBag.SessionID = new SelectList(db.Sessions, "SessionID", "SessionName"); ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FName"); return View(); } // POST: SessionStudents/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "SessionStudentID,SessionID,GradeID")] SessionStudent sessionStudent) { if (ModelState.IsValid) { db.SessionStudents.Add(sessionStudent); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.GradeID = new SelectList(db.Grades, "GradeID", "GradeName", sessionStudent.GradeID); ViewBag.SessionID = new SelectList(db.Sessions, "SessionID", "SessionName", sessionStudent.SessionID); return View(sessionStudent); }

And here is the MultiSelect in the view

<div class="form-group"> @Html.LabelFor(model => model.SessionStudentID, "StudentID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.Kendo().MultiSelectFor(model => model.SessionStudentID).BindTo((SelectList)ViewBag.StudentID).DataTextField("FName").DataValueField("StudentID").Name("SelectedStudentIDs") @Html.ValidationMessageFor(model => model.SessionStudentID, "", new { @class = "text-danger" }) </div> </div>

Any help in this regard is highly appreciated.

最满意答案

问题在于SelectList构造的误解。

当你创建一个SelectList

ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FName");

“StudentID”和“FName”是db.Students集合中的映射字段,而不是 SelectList中的访问器字段名称。 SelectList仍将包含具有从“FName”和“StudentID”映射的“文本”和“值”字段的项目。

所以,当你然后将MultiSelect绑定到

@Html.Kendo().MultiSelectFor(model => model.SessionStudentID) .BindTo((SelectList)ViewBag.StudentID) .DataTextField("FName") .DataValueField("StudentID") .Name("SelectedStudentIDs")

你在说“绑定到列表ViewBag.StudentID,其中”FName“作为文本字段,”StudentID“作为值字段。 ViewBag.StudentID是一个SelectList,它根据定义使用Text和Value。

删除DataTextField和DataValueField说明符应该为您解决它,因为MultiSelectFor将使用默认的“文本”和“值”。

如果您使用BindTo()任意使用Text和Value作为访问器的Enumerable,那么您只需要使用DataTextField和DataValueField,即,如果您没有将db.Students转换为SelectList,以便ViewBag.StudentID是IEnumerable, 那么您需要告诉MultiSelect使用FName和StudentID来访问绑定枚举中的项。

The problem is a misunderstanding of the SelectList contruction.

When you create a SelectList with

ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FName");

"StudentID" and "FName" are the mapping fields in the db.Students collection, NOT the accessor field names in the SelectList. The SelectList will still contain items with "Text" and "Value" fields mapped from "FName" and "StudentID".

So, when you then bind the MultiSelect to

@Html.Kendo().MultiSelectFor(model => model.SessionStudentID) .BindTo((SelectList)ViewBag.StudentID) .DataTextField("FName") .DataValueField("StudentID") .Name("SelectedStudentIDs")

You are saying "Bind to the list ViewBag.StudentID with "FName" as the text field and "StudentID" as the value field. BUT ViewBag.StudentID is a SelectList, which uses Text and Value by definition.

Removing the DataTextField and DataValueField specifiers should fix it for you, as then the MultiSelectFor will use the defaults of "Text" and "Value".

You would only need to use the DataTextField and DataValueField if you BindTo() an arbitrary Enumerable that didn't use Text and Value as the accessors, i.e. if you didn't convert db.Students to a SelectList such that ViewBag.StudentID was an IEnumerable, then you would need to tell the MultiSelect to use FName and StudentID to access the items in the bound enumerable.

更多推荐

本文发布于:2023-07-15 22:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119396.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   项目   Kendo   MultiSelect   load

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!