无法将组合集的值获取到viewmodel

时间:2017-04-12 09:36:55

标签: kendo-ui asp.net-mvc-5 kendo-asp.net-mvc kendo-combobox

我已经实现了剑道下拉的级联功能。在尝试保存信息时,我无法在viewmodel中获取组合的值。如果我注释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 for cascading feature to work

 function GetFilterOption() {

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

    }

Javascript - 尝试设置无效的模型

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

1 个答案:

答案 0 :(得分:0)

使用.ComboBoxFor()时,不应使用.Name()。

使用.ComboBoxFor(m =&gt; m.FieldName)时,id / name属性将设置为&#34; FieldName&#34;通过剑道实施Razor助手和匹配模型字段的名称。

如果您随后使用.Name(&#34; SomeOtherFieldName&#34;),则将id / name属性更改为&#34; SomeOtherFieldName&#34;并且不再匹配模型的字段名称,这就是您不再获得该值的原因。

你想要做的是 not 使用.Name()并适当地设置.CascadeFrom(),即

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

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

function GetFilterOption() {
    return {
        id: $('#CountryCode').val()
    }
}
相关问题