从Kendo MVC combo的DataTextField中检索值并将其分配给viewmodel

时间:2017-04-13 09:04:26

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

我正在我的MVC应用程序中实现MVC kendo组合框控件。 kendo组合框控件包含分别绑定到value和text属性的model属性。 text属性包含名为CompanyCodeCompany的连接模型属性。我基本上需要提取公司代码并将其分配给viewmodel中的公司代码属性,因为我在保存到数据库时需要该值。请注意,我需要国家代码和公司代码。目前,组合框值字段绑定到CountryCode。目前,使用SalesOrganisation viewmodel填充组合框,主表单使用newrequest viewmodel发布数据。我在新请求viewmodel上尝试了CompanyCodeCompany属性。但是那个dobe似乎也工作。我明白组合@(Html.Kendo()。ComboBoxFor(model => model.CountryCode)中的这段代码只会绑定国家代码。理想的方法是检索来自salesorganisation的公司代码值,并直接在razor中将其分配给新请求viewmodel。

我不确定采取何种方法来检索公司代码值。

<div class="form-group">
                @Html.LabelFor(model => model.Company, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-8">
                    <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)
                        .Value(@user.DefaultCountryCode)
                        .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>

销售组织视图模型

 public class SalesOrganisationViewModel
    {
        public string CountryCode { get; set; }

        public string CompanyCode { get; set; }

        [Display(Name = "Sales Organisation")]
        public string Company { get; set; }


        public string CompanyCodeCompany
        {
            get
            {
                return CompanyCode + " - " + Company;
            }
        }

    }

NewRequest ViewModel

public class NewRequestViewModel
    {

        private string name;

        [Display(Name = "Request Id")]
        public int RequestID { get; set; }
        public int CustomerMasterDataId { get; set; }

        [Display(Name = "Customer Number")]
        public int CustomerNumber { get; set; }
        [Display(Name = "Customer Name")]
        public string Name1 { get; set; }
        [Display(Name = "Customer Group")]
        public string CustomerGroup { get; set; }

        public string CountryCode { get; set; }
       public string CompanyCodeCompany { get; set; }
      }

1 个答案:

答案 0 :(得分:0)

与您的其他问题类似:基本上,在组合框中处理更改事件并设置公司代码的值。首先,在您的视图中添加一个HiddenFor:

@Html.HiddenFor(model => model.CompanyCode)

现在处理更改事件:

   @(Html.Kendo().ComboBoxFor(model => model.CountryCode)
    .HtmlAttributes(new { style = "width:100%" })
    .DataTextField("CompanyCodeCompany")
    .DataValueField("CountryCode")
    .Filter("contains")
    .MinLength(3)
    .Value(@user.DefaultCountryCode)
    .Events(e => e.Change(onComboChange))
    .DataSource(dataSource => dataSource
    .Read(read => read.Action("RequestHeader_SalesOrganisation", "Request").Type(HttpVerbs.Post))
    .ServerFiltering(true)
    )

然后访问dataitem并设置值:

@section scripts
{
    <script type="text/javascript">
        function onComboChange(e) {
            var dataItem = e.sender.dataItem();

            if (dataItem) {
                $("#CompanyCode").val(dataItem.CompanyCode);
            };
        };

    </script>
}

现在国家代码和公司代码都应该回发。以下是绑定部分如何工作的示例:http://jsfiddle.net/sg53719/74LwhebL/1/