为什么视图没有刷新?(.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);
    }
}

默认操作显示所有产品线。SupplierDropUsed在dropdownlist更改时调用。

视图:

@model RyfMvcTestApplication1.Models.SupplierModel

@{Layout=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和控制器操作(我在调试模式下进行了检查)。我也得到了正确的供应商id。但视图永远不会刷新。我仍然可以看到所有产品线的列表。

您通过提交请求的jquery post方法执行post,这就是为什么您的调试操作被调用,但post调用返回的结果从未用于更新UI。

最新更新