MVC2 操作结果在下拉列表更改时未执行

本文关键字:执行 下拉列表 操作 结果 MVC2 | 更新日期: 2023-09-27 17:56:47

我正在学习MVC 2 ASP.NET 遇到了一个小问题,我似乎无法弄清楚。

基本上,我有一个从 Access db 填充的下拉列表,当用户选择一个值时,我想调用一个将所选值作为参数传递的 ActionResult,我正在尝试通过使用$.post来实现这一点

下拉列表正在填充数据,我可以确认 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>

MVC2 操作结果在下拉列表更改时未执行

我的javascript函数中有一个语法错误,这修复了它:

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