我有这段代码,但是当我更改第一个选项时DropDownList
,出现以下错误:
在第二个 DropDownList 中未定义
-El controlador-
public JsonResult ArticulosPorCategoria(int Id )
{
var articulos = db.articulo.ToList().Where(p => p.Id_Categoria == Id);
return Json(articulos, JsonRequestBehavior.AllowGet);
}
<script type="text/javascript">
$(function () {
$("#Categoria").change(function () {
var val = $(this).val();
var subItems="";
$.getJSON("@Url.Action("ArticulosPorCategoria","Ventas")", {id:val} ,function (data){
$.each(data,function(index,item){
subItems+="<option value='"+item.Value+"'>"+item.Text+"</option>"
});
$("#Articulo").html(subItems)
});
});
});
</script>
@Html.DropDownList("Categoria", ViewBag.Categoria as SelectList,
htmlAttributes: new { @class = "form-control" })
@Html.DropDownList("Articulo", ViewBag.Artics as SelectList,
htmlAttributes: new { @class = "form-control" })
我已经解决了,以防将来有人需要类似的东西
控制器
脚本