asp.net-mvc - 使用 MVVM 将 kendo.data.DataSource 绑定(bind)到组合

标签 asp.net-mvc asp.net-mvc-4 mvvm kendo-asp.net-mvc kendo-mvvm

又是下一个问题,这次是一个棘手的问题,

数据源:

var dsCountryList =
       new kendo.data.DataSource({
           transport: {
               read: {
                   dataType: "jsonp",
                   url: "/Masters/GetCountries"
               }
           },
           schema: {
               model: {
                   id: "CountryID",
                   fields: {
                       "CountryDesc": {

                       }
                   }
               }
           }
       });

可观察对象
function Set_MVVMSupplier() {
    vmSupplier = kendo.observable({
        SupplierID: 0,
        SupplierName: "",
        AccountNo: "",
        CountryList: dsCountryList,
});

    kendo.bind($("#supplierForm"), vmSupplier);
}

这是绑定(bind)到可观察对象的 html,但我没有填充组合框,而且每次我单击组合请求时都会转到服务器并为 countryID、CountryDesc 带来 json 格式的数据
 <div class="span6">
                <div class="control-group">
                    <label class="control-label" for="txtCountryId">Country</label>
                    <div class="row-fluid  controls">
                        @*<input class="input-large" type="text" id="txtCountryId" placeholder="CountryId" data-bind="value: CountryId">*@
                        <select  id="txtCountryId" data-role="dropdownlist"
                             data-text-field="CountryDesc" data-value-field="CountryID" , data-skip="true"
                             data-bind="source: CountryList, value: CountryDesc">
                        </select>
                    </div>
                </div>
            </div>

最佳答案

我没有得到答案,所以我找到了代码的替代工作冰。看看,如果有帮助,请投票。

在 js 文件中为 ddl 创建模型

 ddl = kendo.data.Model.define({
    fields: {
        CountryId: { type: "int" },
        ConfigurationID: { type: "int" }
    }
});

将 var ddl 添加到 MVVM js 文件
 vmSupplier = kendo.observable({
    CountryId: new ddl({ CountryId: 0 }),
    ConfigurationID: new ddl({ ConfigurationID: 0 }),});

在 Controller 中添加代码
 using (CountriesManager objCountriesManager = new CountriesManager())
        {
            ViewBag.Countries = new SelectList(
                objCountriesManager.GetCountries().Select(p => new { p.CountryID, p.CountryDesc })
                , "CountryID", "CountryDesc"); ;
        }

在cshtml中添加代码
      <div class="span4">
                        <label class="control-label" for="txtCountryId">Country</label>
                        @Html.DropDownList("Countries", null,
                      new System.Collections.Generic.Dictionary<string, object> {
                      {"id", "txtCountryId" },
                      { "data-bind","value: CountryId"} })

                    </div>

这样我就解决了问题

关于asp.net-mvc - 使用 MVVM 将 kendo.data.DataSource 绑定(bind)到组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18802170/

相关文章:

C# MVC 4 ASP.net,如何将文件插入数据库存储(二进制)

asp.net-mvc - 在 MVC4 中将 Ninject 与 SignalR 结合使用

wpf - 您是如何在 MVVM 中成功实现 MessageBox.Show() 功能的?

silverlight - 如何使用 Silverlight 和 MVVM 设计复合 View 和 View 模型?

c# - 跟踪广告展示次数(浏览次数)并防止欺诈

asp.net-mvc - 从 FormCollection 元素获取多个复选框

c# - MVC : Ace editor post

asp.net - 使用 jquery.load 将对象数组传递给 MVC 3

c# - 为什么 SuppressFormsAuthenticationRedirect 导致构建错误?

c# - “OneTimeSetUp: No suitable constructor was found”与ApplicationDbContext的集成测试