0

私のアプリケーションは asp.net MVC で、Telerik MVC Combobox をモデルにバインドしようとしています。モデルは次のとおりです。

public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool DisplayBold { get; set; }
        public string Value
        {
            get
            {
                return string.Format("{0}|{1}", this.Id, this.DisplayBold.ToString());
            }
        }
    }

コントローラーで:

  var people = new List<Person>();
        people.Add(new Person { Id = 1, Name = "John Doe", DisplayBold = true });
        people.Add(new Person { Id = 2, Name = "Jayne Doe", DisplayBold = false });
        ViewData["people"] = people;
        return View();

私は値を取得します。

ビューで:

<%= Html.Telerik().ComboBox()
       .Name("ComboBox")
           .BindTo((IEnumerable<SelectListItem>)ViewData["people"])
%>

次のエラーが表示されます。

Unable to cast object of type 'System.Collections.Generic.List`1[caseprog.Models.Person]' to type 'System.Collections.Generic.IEnumerable`1[System.Web.Mvc.SelectListItem]'.

ご提案いただければ幸いです。前もって感謝します。

4

1 に答える 1

1

IEnumerableList of People をofにキャストすることはできませんSelectListItem。それらは2つの異なるものです。

代わりに、リストを のリストに変換する必要がありますSelectListItem。いくつかの方法でそれを行うことができますが、これはうまくいくはずです:

.BindTo(new SelectList((IEnumerable<Person>)ViewData["people"], "Id", "Name"))
于 2012-09-22T19:19:13.420 に答える