ASP.net mvc 对下拉值更改的调用操作

发布于 2024-09-27 05:10:53 字数 254 浏览 3 评论 0原文

我有一个关于我的观点的下拉列表。该下拉列表仅适用于条目。基本上我需要知道如何在下拉值更改时调用操作?

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

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

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

提前致谢

Ive got a dropdown on one of my views. This dropdown only has for entries. Basically i need to know how to call an action when the dropdown value is changed?

My situation is: Im making a simple inbox page. The dropdown has the filter options: View All, View Invites, View Replies etc..

When the user selects a filter option from the dropdown I want to call to an action to return the new view with the filtered data.

Any ideas? Im guessing it is somehow going to be a script attached to the OnChange of the dropdown, but i wouldnt have a clue what the syntax is or how call MVC action from the script.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2024-10-04 05:10:53

为此,您需要使用 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() }
        );
    });
});

You need to use javascript for this. Here's an example. Suppose you have the following view model:

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

which you would populate in your controller:

[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);
    }
}

And then the view which is strongly typed to this model:

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

The last part is to register for the change event (using jquery in this example):

$(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() }
        );
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文