【发布时间】:2017-06-11 14:38:22
【问题描述】:
我有一个级联下拉菜单,到目前为止效果很好。现在我需要填充如下所示的文本框。
AJAX 代码
$('#CommonHerpsDropdown').change(function () {
$('#SpeciesDropdown').empty();
$.ajax({
type: "POST",
url: '@Url.Action("GetGenericSpeciesByCommonId")',
datatype: "Json",
data: { cAnimalId: $('#CommonHerpsDropdown').val() },
success: function (genericSpecies) {
$.each(genericSpecies, function (index, value) {
$('#SpeciesDropdown').append('<option value="' + value.SpeciesId + '">' + value.StandardName + '</option>');
$('#ScientificClass').val(value.ScientificClassification);
$('#ScientificOrder').val(value.ScientificOrder);
$('#ScientificFamily').val(value.ScientificFamily);
$('#ScientificGenus').val(value.ScientificGenus);
$('#ScientificSpecies').val(value.ScientificSpecies);
});
}
});
});
MVC 控制器 Json 方法
public JsonResult GetGenericSpeciesByCommonId(int cAnimalId)
{
//Lets get the species data based on CommonHerpID.
var genericSpecies = (from x in _context.ReptileSpeciesList
where x.CommonHerpId == @cAnimalId
select x).OrderBy(x => x.StandardName);
return Json(genericSpecies);
}
我的问题是如何设置分类和顺序字段以在物种选择发生变化时填充/重新填充而不使用对 db 表的另一个调用。
【问题讨论】:
-
当前代码有什么问题?您似乎也在填充文本框
-
代码有效。但我试图看看是否有办法避免 $('#SpeciesDropdown').change(functon(){ //Ajax 调用和字段绑定)} 所以我不必绑定这些文本字段多个单独更改函数中的时间
-
参考this answer中的第二个和第三个选项
标签: json ajax asp.net-mvc