0
我正在學習.Net MVC。我有一個頁面顯示產品線。我想通過下拉列表來篩選供應商的產品線。爲什麼視圖不刷新? (.Net MVC)
我的控制器:
public class ProductlineController : Controller
{
SupplierRepository sr = new SupplierRepository();
ProductlineRepository pr = new ProductlineRepository();
public ActionResult Default()
{
SupplierModel sm = new SupplierModel();
List<Supplier> suppliers = sr.GetAll();
sm.Suppliers = (from s in suppliers select new SelectListItem {
Text = s.Name,
Value = s.Id.ToString()
}).ToList();
sm.Productlines = pr.GetAll();
return View("List", sm);
}
[HttpPost]
public ActionResult SupplierDropUsed(int id)
{
SupplierModel sm = new SupplierModel();
List<Supplier> suppliers = sr.GetAll();
sm.Suppliers = (from s in suppliers
select new SelectListItem
{
Text = s.Name,
Value = s.Id.ToString()
}).ToList();
Supplier supplier = sr.GetById(id);
sm.Productlines = supplier.Productlines.ToList();
return View("List", sm);
}
}
的默認操作顯示所有代理產品。當dropdownlist被改變時,SupplierDropUsed被調用。
的視圖:
@model RyfMvcTestApplication1.Models.SupplierModel
@ { 佈局= NULL; }
列表
<script type="text/javascript">
function supplierDropChanged() {
$.post("Productline/SupplierDropUsed", { id: $('#SupplierDrop').val() });
}
</script>
<div><strong>Filter by supplier</strong></div>
<br />
<div>
@Html.DropDownList("SupplierDrop", Model.Suppliers, "Select supplier", new { onChange = "supplierDropChanged()" })
</div>
<br />
<br />
<table>
<tr>
<th style="width:50px; text-align:left">Id</th>
<th style="text-align:left">Name</th>
<th style="text-align:left">Supplier</th>
</tr>
@foreach (var item in Model.Productlines) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.Name)
</td>
</tr>
}
</table>
當我選擇供應商時,JavaScript和控制器操作被執行(I在調試模式下選中)。我也得到正確的供應商ID。但這個觀點從來沒有刷新過。我仍然看到所有產品線的清單。
是不是隻是描述問題? – SteveCav 2015-06-29 06:46:38