无法将组合设置的值设置为视图模型

问题描述:

我已经实现了kendo下拉的级联功能。在尝试保存信息时,我无法在视图模型中获得组合值。如果我评论name属性,我确实得到了这个值,但是我需要name属性才能使用级联功能。我正在尝试使用jquery来设置模型值但获取错误的解决方法。有人可以告诉我如何解决这个问题。无法将组合设置的值设置为视图模型

销售组织组合

<div class="form-group"> 
      @Html.LabelFor(model => model.Company, htmlAttributes: new { @class = "control-label col-md-4" }) 
      <div class="col-md-6"> 
       <div class="editor-field"> 
        @(Html.Kendo().ComboBoxFor(model => model.CountryCode) 
    .Name("SalesOrganisation") 
    .HtmlAttributes(new { style = "width:100%" }) 
    .DataTextField("CompanyCodeCompany") 
    .DataValueField("CountryCode") 
    .Filter("contains") 
    .MinLength(3) 
    .DataSource(dataSource => dataSource 
    .Read(read => read.Action("RequestHeader_SalesOrganisation", "Request").Type(HttpVerbs.Post)) 
    .ServerFiltering(true) 
    ) 

        ) 
       </div> 
       @Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

销售办事处组合

<div class="form-group"> 
        @Html.LabelFor(model => model.SalesOffice, htmlAttributes: new { @class = "control-label col-md-4" }) 
        <div class="col-md-6"> 
         <div class="editor-field"> 
          @(Html.Kendo().ComboBoxFor(model => model.SalesOfficeID) 
      // .Name("SalesOffice") 
      .HtmlAttributes(new { style = "width:100%" }) 
      .DataTextField("SalesOffice") 
      .DataValueField("SalesOfficeID") 
      .AutoBind(false) 
      .Value("") 
      .DataSource(dataSource => dataSource 
       .Read(read => 
       { 
        read.Action("RequestHeader_SalesOffice", "Request") 
         .Type(HttpVerbs.Post) 
         .Data("GetFilterOption"); 
       }).ServerFiltering(true) 
       ).CascadeFrom("SalesOrganisation").Filter("contains") 

          ) 
         </div> 
         @Html.ValidationMessageFor(model => model.SalesOffice, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

的Javascript级联功能起作用

function GetFilterOption() { 

     return { 
      id: $('#SalesOrganisation').val() 
     } 

    } 

使用Javascript - 尝试设置其不工作

function GetFilterOption() { 
     var countryCode = $('#SalesOrganisation').val(); 
     var model = $('#SalesOrganisation').data('kendoComboBox'); 
     model.value = countryCode; 
     return id = countryCode; 
    } 

当您使用.ComboBoxFor(),你不应该使用,请将.Name()模型。

当您使用.ComboBoxFor(M => m.FieldName)则使用id/name属性将被剑道的实施剃刀助手和比赛模型字段的名称设置为“字段名”。

如果再使用,请将.Name(“SomeOtherFieldName”),更改ID /名称属性为“SomeOtherFieldName”,它不再你的模型,这就是为什么你不再得到值的字段名相匹配。

你想要做什么是使用,请将.Name(),并成立了.CascadeFrom()适当,即

@(Html.Kendo().ComboBoxFor(model => model.CountryCode) 
.... 

@(Html.Kendo().ComboBoxFor(model => model.SalesOfficeID) 
    .CascadeFrom("CountryCode") 

function GetFilterOption() { 
    return { 
     id: $('#CountryCode').val() 
    } 
} 
+0

是的,这工作,但我有一个用于过滤基于这个组合的另一组合' 文本域。我该怎么做 function GetFilterOption1(){ return {id:$('[name =“SalesOrganisation”]')。data(“kendoComboBox”).text().span(' - ')[0] } } – Tom

+0

我做了相同的分配其他过滤器选项countrycode,它似乎工作。这是正确的做法 – Tom

+0

是的,你必须通过一个单一和一致的名称来引用你的输入/元素/小部件,当你使用XXXXFor()剃刀助手时,这是你的ViewModel字段的名称。 –