为什么视图不刷新? (.Net MVC)

问题描述:

我正在学习.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。但这个观点从来没有刷新过。我仍然看到所有产品线的清单。

您正在通过提交请求的jquery post方法进行发布,这就是为什么您的调试操作被调用,但从post调用返回的结果永远不会用于更新您的用户界面。

+0

是不是只是描述问题? – SteveCav 2015-06-29 06:46:38