我已经采用实体模型。我需要根据第一个下拉列表绑定第二个下拉列表。我可以将第一个下拉选择的值传递给控制器操作,但我无法在第二个下拉列表中显示值。这是我的视图代码,COGPHome .cshtml
<script type="text/javascript">
$(document).ready(function () {
$('#productDetails').change(function () {
//var productId = document.getElementById('ProductDetails');
var productId = $(this).val();
var prod = { ProductId: productId };
$.ajax({
url: '/COGP/GetProductFamily',
type: 'POST',
data: prod,
success: function (data) {
}
});
});
})
<h2>@ViewBag.Title</h2>
<div id="dvfilter">
<table align="center">
<tr>
<td>
@Html.Label("Product:")
</td>
<td>
@Html.DropDownList("productDetails", "---select---")
</td>
<td>
@Html.Label("ProductFamily:")
</td>
<td>
@Html.DropDownList("productFamily", "---select---")
</td>
</tr>
</table>
这是控制器代码
public class COGPController : Controller
{
COGPEntities objEnt = new COGPEntities();
public ActionResult COGPHome()
{
List<refProduct> objListproducts = new List<refProduct>();
objListproducts = (from c in objEnt.refProducts select c).ToList();
ViewData["productDetails"] = new SelectList(objListproducts, "ProductId", "Product");
ViewData["productFamily"] = new SelectList(string.Empty);
return View();
}
public ActionResult GetProductFamily(int productId)
{
List<refProductFamily> objListfamily = new List<refProductFamily>();
objListfamily = (from f in objEnt.refProductFamilies select f).ToList();
ViewData["productFamily"] = new SelectList(objListfamily, "ProductFamilyId", "ProductFamily");
return View();
}
}