2013-01-31 29 views
1

我正在學習ASP.NET MVC 2,遇到了一個我似乎無法弄清的輕微問題。我試圖通過使用$.post來實現這一點當DropDownList發生變化時,MVC2 ActionResult不能執行

下拉是越來越填充數據,我可以證實,javascript函數的火災,但在我的ActionResult破發點仍然沒有被擊中,我會很感激任何意見:

型號:

public class SuppliersModel 
{ 
    public SuppliersModel() 
    { 
    } 

    public SuppliersModel(string id) 
    { 
    } 

    private string id; 
    public string ID 
    { 
     get { return id; } 
     set { id = value; } 
    } 

    public SelectList OurSuppliers 
    { 
     get 
     { 
      List<SelectListItem> suppliers = GetSuppliers(); 
      var list = new SelectList(suppliers, "Value", "Text"); 
      return list; 
     } 
    } 

    public IEnumerable<string> UniqueSuppliers{get;set;} 

    private List<SelectListItem> GetSuppliers() 
    { 
     var suppliers = new List<SelectListItem>(); 
     string conn = WebConfigurationManager.ConnectionStrings["connection"].ConnectionString; 

     using (var connection = new OleDbConnection(conn)) 
     { 
      connection.Open(); 
      string commandText = "SELECT [Supplier], [ID] FROM [Suppliers] ORDER BY [Supplier]"; 
      using (var command = new OleDbCommand(commandText, connection)) 
      { 
       command.CommandType = System.Data.CommandType.Text; 
       using (OleDbDataReader reader = command.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         string id = reader["ID"].ToString(); 
         string supplier = reader["Supplier"].ToString(); 
         suppliers.Add(new SelectListItem() { Text = supplier, Value = id }); 
        } 
       } 
      } 
      connection.Close(); 
      return suppliers; 
     } 
    } 
} 

控制器:

public class SuppliersController : Controller 
{ 
    public ActionResult Suppliers() 
    { 
     SuppliersModel model = new SuppliersModel(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult SuppliersById(string id) 
    { 
     System.Diagnostics.Debugger.Break(); 
     SuppliersModel model = new SuppliersModel(id); 
     return View(model); 
    } 
} 

查看:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <script type="text/javascript"> 
     $(function() { 
      $('#suppliers').change(function() { 
       debugger; 
       $.post('@Url.Action("SuppliersById", "Suppliers")', { id: $(this).val() }, 
       function (result) { 
       }); 
      }); 
     }); 
    </script> 
    <h2> 
     Suppliers</h2> 
    <div> 
     <%= Html.DropDownList("suppliers",Model.OurSuppliers) %> 
    </div> 
    <% if (Model.ID != null) 
     { %> 
    <table> 
     <tr> 
      <th> 
       <h2> 
       List of suppliers 
       </h2> 
      </th> 
     </tr> 
     <% foreach (var item in Model.UniqueSuppliers) 
      { %> 
     <tr> 
      <td> 
       <%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> 
       | 
       <%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%> 
       | 
       <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%> 
      </td> 
     </tr> 
     <% } %> 
    </table> 
    <% } %> 
</asp:Content> 
+1

可能是值得加入掠影(http://getglimpse.com/),它會告訴你它是否是routi ng issue ... –

+3

嘗試在Fiddler中查看帖子,你應該看到它將要發佈的路線以及發佈的內容。您也可以嘗試在提琴手中構建帖子並張貼這種方式。 – Dom

+1

打開您的瀏覽器的JavaScript控制檯(F12)並查看它嘗試發佈到的位置。也許你因爲一些路由問題而得到404,而你不知道它。另外,您的POST方法需要返回Json()而不是View()。 View()將返回HTML,我不認爲這是你想要的。 – hawkke

回答

1

有我的JavaScript函數的語法錯誤,這個固定:

<script type="text/javascript"> 
    $(function() { 
     $('#suppliers').change(function() { 
      $.post('<%=Url.Action("SuppliersById", "Suppliers")%>', { id: $(this).val() }, 
      function (result) { 
      }); 
     }); 
    }); 
</script> 
相關問題