【发布时间】:2017-04-05 11:07:21
【问题描述】:
我正在尝试根据对另一个组合框的选择来填充一个组合框的值。我在 MVC 5 应用程序中使用 kendo mvc 组合框。就我而言,我正在尝试根据 SalesOrganisation 组合框的选择填充销售办公室组合框。为此,我需要调用 SalesOffice 组合的控制器方法并传递国家代码值。我已经为销售组织的下拉控件的更改事件编写了一个 ajax 方法。它调用控制器方法。我可以看到该方法正在触发,但是当我对 javascript 代码中的数据发出警报时,该值显示 [object] [object]。但是状态显示成功不知道出了什么问题。如何填充销售办公室下拉列表
组合框
<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.Company)
.Name("SalesOrganisation")
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("Company")
.DataValueField("CountryCode")
.DataSource(dataSource => dataSource
.Read(read => read.Action("RequestHeader_SalesOrganisation", "Request").Type(HttpVerbs.Post))
)
.Events(e =>
{
e.Change("onChange");
})
)
</div>
@Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" })
</div>
</div>
<div class="clearfix"></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.SalesOffice)
.Name("SalesOffice")
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("SalesOffice")
.DataValueField("SalesOfficeID")
.DataSource(dataSource => dataSource
.Read(read => read.Action("RequestHeader_SalesOffice", "Request").Type(HttpVerbs.Post))
)
)
</div>
@Html.ValidationMessageFor(model => model.SalesOffice, "", new { @class = "text-danger" })
</div>
</div>
SalesOffice 控制器方法
public ActionResult RequestHeader_SalesOffice(string id)
{
var response = requestRepository.GetSalesOffice(id).AsQueryable().ProjectTo<SalesOfficeViewModel>();
var jsonResult = Json(response, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
jQuery
function onChange() {
alert($('#SalesOrganisation').val());
var ServiceUrl = "/CC.GRP.MCRequest/Request/RequestHeader_SalesOffice?id=" + $('#SalesOrganisation').val();
var content = '';
$.support.cors = true;
$.ajax({
type: 'Post',
url: ServiceUrl,
async: true,
cache: false,
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: 'json',
error: function (xhr, err) {
},
success: function (data, status) {
$('#SalesOffice').val(data);
alert(data);
alert(status);
}
});
}
【问题讨论】:
-
你看过这个例子来了解如何做级联组合的demos.telerik.com/kendo-ui/combobox/cascadingcombobox
-
您分享的示例是使用 odata。在我的代码中,我试图调用确实被触发的控制器操作方法。我可以看到它在服务器端获取数据。我的问题是如何在客户端获取该 json 数据并绑定它
-
您要触发的更改事件是获取组合框列表中的值还是拉回新的
select列表以绑定到组合框? -
它应该绑定新的选择列表。它调用我的控制器代码,该代码包含在帖子中。它根据国家代码过滤列表。它应该将过滤后的列表绑定到组合框。我不确定进行 Ajax 调用是否正确。欢迎任何更好的实施。如果不是想知道为什么我当前的代码没有捕获列表。
标签: jquery asp.net-mvc kendo-ui kendo-combobox