【问题标题】:ASP.net mvc Call Action on DropDown Value ChangeASP.net mvc 对下拉值更改的调用操作
【发布时间】:2023-03-04 17:53:01
【问题描述】:

我有一个关于我的观点的下拉菜单。此下拉列表仅适用于条目。基本上我需要知道当下拉值改变时如何调用一个动作?

我的情况是:我正在制作一个简单的收件箱页面。下拉列表有过滤选项:查看全部、查看邀请、查看回复等。

当用户从下拉列表中选择过滤器选项时,我想调用一个操作以返回带有过滤数据的新视图。

有什么想法吗?我猜它会以某种方式成为附加到下拉列表的 OnChange 的脚本,但我不知道语法是什么或如何从脚本中调用 MVC 操作。

提前致谢

【问题讨论】:

    标签: asp.net-mvc-2 drop-down-menu client-side


    【解决方案1】:

    您需要为此使用 javascript。这是一个例子。假设您有以下视图模型:

    public class MyViewModel
    {
        public IEnumerable<SelectListItem> Values { get; set; }
    }
    

    您将在控制器中填充的内容:

    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Values = new[] 
                {
                    new Item { Value = "1", Text = "Item 1" },
                    new Item { Value = "2", Text = "Item 2" },
                    new Item { Value = "3", Text = "Item 3" }
                }
            };
            return View(model);
        }
    }
    

    然后是这个模型的强类型视图:

    <%: Html.DropDownListFor(x => x.SelectedValue, 
        new SelectList(Model.Values, "Value", "Text"), 
        new { id = "items" }
    )%>
    

    最后一部分是注册更改事件(本例使用jquery):

    $(function () {
        // Register for the change event of the drop down
        $('#items').change(function () {
            // When the value changes, get send an AJAX request to the
            // Filter action passing the selected value and update the
            // contents of some result div with the partial html returned
            // by the controller action
            $('#result').load('<%: Url.Action("filter") %>', 
                { selectedValue: $(this).val() }
            );
        });
    });
    

    【讨论】:

    • 同一路径上的另一个问题。我的邮箱列表由 DevExpress 网格组成。每行都有一个用户可以选中的复选框,以便他们可以一次删除多条记录。当用户单击全选链接或清除所有链接时,我需要将网格中的所有复选框设置为选中或取消选中。我如何使用客户端脚本来解决这个问题?谢谢
    • 耶稣,我喜欢网络表单
    • 使用这个来使用jquery序列化所有表单数据并调用ajax stackoverflow.com/a/2019614/184572
    猜你喜欢
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多