1

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

基本上我有一个从 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>
4

1 回答 1

1

我的 javascript 函数中存在语法错误,这修复了它:

<script type="text/javascript">
    $(function () {
        $('#suppliers').change(function () {
            $.post('<%=Url.Action("SuppliersById", "Suppliers")%>', { id: $(this).val() }, 
            function (result) {
            });
        });
    });
</script>
于 2013-01-31T21:13:17.847 回答