【问题标题】:Binding dynamic data to kendo Dropdownlist将动态数据绑定到 kendo Dropdownlist
【发布时间】:2014-09-16 12:11:01
【问题描述】:

我正在尝试将剑道下拉列表与模型绑定,但我在每个选项中都未定义。

型号:

public SelectList CountriesTemp { get; set; }
public int Country{get;set;}

控制器:

  public ActionResult Registration()
{
RegistrationModel Model = new RegistrationModel();
Model.CountriesTemp = new SelectList(ObjService.GetCountries(), "CountryID", "Country_Name");
return View(Model);
}

查看页面

@(Html.Kendo().DropDownListFor(m => m.Country)
        //The name of the dropdownlist is mandatory. It specifies the "id" attribute of the widget.
  .DataTextField("Country_Name") //Specifies which property of the Product to be used by the dropdownlist as a text.
  .DataValueField("CountryID") //Specifies which property of the Product to be used by the dropdownlist as a value.
  .BindTo(Model.CountriesTemp )   //Pass the list of Products to the dropdownlist.
)

有人可以指导我哪里错了,因为如果我绑定一个简单的 MVC 下拉列表,它工作得很好。只需在 ViewPage 中更改如下所示的一行即可。

@Html.Dropdownlist("CountriesTemp")

【问题讨论】:

  • 代替Model.CountriesTemp = new SelectList(ObjService.GetCountries(), "CountryID", "Country_Name") 试试Model.CountriesTemp = new ObjService.GetCountries();。我不认为Kendo.DropDownListFor() 使用SelectList
  • 如果可以的话,它可能需要.DataTextField("Name").DataValueField("Value"),因为它们是SelectList 的唯一(相关)属性

标签: asp.net-mvc-3 asp.net-mvc-4 kendo-ui kendo-asp.net-mvc


【解决方案1】:

您似乎遇到了冲突(可以这么说)。试试这样的:

@(Html.Kendo().DropDownListFor(m => m.Country)
  .DataTextField("Text")
  .DataValueField("Value") // These may be optional now
  .BindTo(Model.CountriesTemp))

或者,您不能在模型上使用SelectList 并执行IEnumerable<Country> Countries { get; set; } 之类的属性。然后绑定看起来像:

@(Html.Kendo().DropDownListFor(m => m.Country)
  .DataTextField("Country_Name")
  .DataValueField("CountryID")
  .BindTo(Model.Countries))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2012-06-08
    相关资源
    最近更新 更多