我有2个下拉列表。第二个下拉列表取决于第一个下拉列表的选定值。值来自数据库中的目录。当第一个下拉列表更改时,控制器从db获取值,并填充de second下拉列表。在此之前,工作正常。
我的控制器
public ActionResult GetDimensionListBox(string cveTipoRepresentante)
{
AjaxResponse ajaxResponse = new AjaxResponse();
var items =
try
{
ajaxResponse.Data = new DbClass.Getitems(cveTipoRepresentante);
ajaxResponse.Success = true;
}
catch (Exception ex)
{
ajaxResponse.Data = null;
ajaxResponse.Success = false;
ajaxResponse.Message = ex.Message;
}
return this.Json(ajaxResponse, JsonRequestBehavior.AllowGet);
}
我的cshtml
<select class="form-control" id="cmbRepreTipo" name="cmbRepreTipo">
<option value="0">Selecciona una opción</option>
@foreach (var repreTipo in Model.RepreTipoList)
{
<option value=@repreTipo.CveInt>@repreTipo.Description</option>
}
</select>
<select class="form-control" id="cmbCveDimension" name="cmbCveDimension">
<option value="0">Seleccione una opción</option>
</select>
我的javascript
$(document).ready(function () {
$(document).on('change', '#cmbRepreTipo', function () {
var valor = $('#cmbRepreTipo option:selected').text();
ChangeComboBoxContent(valor);
});
}
);
function ChangeComboBoxContent(valor)
{
$.ajax({
type: 'GET',
async:'false',
url: '../EvalRepreConf/GetDimensionListBox',
data: { cveTipoRepresentante: valor },
error: function (xhr, ajaxOptions, thrownError) {
swal('error', xhr.status + ' ' + thrownError, 'error');
}
}).done(
function (ajaxResponse) {
if (ajaxResponse.Success) {
BindAjaxResponseDataToComboBox(ajaxResponse);
}
else {
swal('Error', ajaxResponse.Message, 'error');
}
}
);
};
function BindAjaxResponseDataToComboBox(ajaxResponse) {
$('#cmbCveDimension').empty();
var options = $("#cmbCveDimension");
$.each(ajaxResponse.Data, function () {
options.append($("<option />").val(this.CveString).text(this.Description));
});
}
现在,我想从数据库中的记录中设置两个下拉选择值及其各自的值。从具有data-id属性的按钮触发GetElement函数。
function GetElement(elemento) {
var id = $(elemento).attr('data-id');
$.ajax({
type: 'GET',
url: '../EvalRepreConf/GetById',
data: { cveEvalRepreConf: id },
error: function (xhr, ajaxOptions, thrownError) {
swal('error', xhr.status + ' ' + thrownError, 'error');
}
}).done(
function (ajaxResponse) {
if (ajaxResponse.Success) {
BindAjaxResponseDataToForm(ajaxResponse);
}
else {
swal('Error', ajaxResponse.Message, 'error');
}
}
);
}
function BindAjaxResponseDataToForm(ajaxResponse) {
$('#cmbRepreTipo').val(ajaxResponse.Data.CveRepreTipo);
ChangeComboBoxContent(ajaxResponse.Data.CveRepreTipo);
$('#cmbCveDimension').val(ajaxResponse.Data.CveDimension);
};
但是第二次下拉没有填充,也从未设置过。 我做错了吗?