下拉选项更改时更新表

发布于 2024-12-27 10:46:57 字数 509 浏览 3 评论 0原文

我有一个表的一部分,其值取决于下拉列表中的值。当下拉列表的选定选项发生更改时,如何更新表格。

网格

@foreach (var choice in ViewData.Model.Options where choiceId == ViewData.Model.choiceLevelId)
                    {
                        <tr class='gridrow'>
                            <td>.....

下拉菜单

 <div class="editor-field">
                @Html.DropDownListFor(model => model.choiceLevelId,
                            (
                             ...

I have a portion of a table whose values depend on the value in my dropdownlist. How can I update the the table when the selected option of the dropdownlist changes.

grid

@foreach (var choice in ViewData.Model.Options where choiceId == ViewData.Model.choiceLevelId)
                    {
                        <tr class='gridrow'>
                            <td>.....

dropdown

 <div class="editor-field">
                @Html.DropDownListFor(model => model.choiceLevelId,
                            (
                             ...

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

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

发布评论

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

评论(1

回首观望 2025-01-03 10:46:57

你说的是哪张桌子? ASP.NET MVC 不了解桌子或椅子。它适用于模型、控制器和视图。

因此,您有一个视图,其中呈现了一个

您必须在视图中使用 javascript 才能订阅此下拉列表的更改事件。然后您有几种可能性:

  • 使​​用 AJAX 请求将所选值发送到服务器。
  • 将当前浏览器(使用 window.location.href)重定向到控制器操作,并将所选值作为操作参数传递给它。

根据您的要求,您可以选择最适合您的技术。

让我用 jQuery 的示例来说明第一种方法:

<script type="text/javascript">
    $(function() {
        // subscribe to the .change event of the dropdowns
        $('select').change(function() {
            // fetch the newly selected value
            var selectedValue = $(this).val();
            // send it as an AJAX request to some controller action
            $.post('@Url.Action("Foo")', { value: selectedValue }, function(result) {
                // this will be invoked in the case of success of the AJAX call
                // TODO: do something with the results returned by the controller action
            });
        });
    });
</script>

以及将处理 AJAX 请求的控制器操作:

[HttpPost]
public ActionResult Foo(string value)
{
    // this action will be invoked when the user changes the selection
    // in one of the dropdown lists and passed the newly selected value.
    // So using this new value you could perform the desired processing
    // on your model here and return a view. Since this action was invoked
    // with an AJAX call you could return a JSON string to the client indicating
    // the success or failure of whatever processing you were intending to perform 
    // here

    ...

}

更新:

如果您想在选择更改时提交包含下拉列表的表单,您可以使用以下命令:

<script type="text/javascript">
    $(function() {
        // subscribe to the .change event of the dropdowns
        $('select').change(function() {
            // trigger the submission of the containing form:
            $(this).closest('form').submit();
        });
    });
</script>

What table are you talking about? ASP.NET MVC has no knowledge of tables or chairs. It works with Models, Controllers and Views.

So you have a View in which you have rendered a <select> box and you want upon changing the value of this select box by the user to invoke a Controller Action passing the newly selected value to the server so that it can perform some processing. Am I right?

You will have to use javascript in your View in order to subscribe to the change event of this dropdown. Then you have a couple of possibilities:

  • Use an AJAX request to send the selected value to the server.
  • Redirect the current browser (using window.location.href) to the controller action passing it the selected value as action argument.

Depending on your requirements you could pick the technique that suites best for you.

Let me illustrate the first approach with an example using jQuery:

<script type="text/javascript">
    $(function() {
        // subscribe to the .change event of the dropdowns
        $('select').change(function() {
            // fetch the newly selected value
            var selectedValue = $(this).val();
            // send it as an AJAX request to some controller action
            $.post('@Url.Action("Foo")', { value: selectedValue }, function(result) {
                // this will be invoked in the case of success of the AJAX call
                // TODO: do something with the results returned by the controller action
            });
        });
    });
</script>

and your controller action that will handle the AJAX request:

[HttpPost]
public ActionResult Foo(string value)
{
    // this action will be invoked when the user changes the selection
    // in one of the dropdown lists and passed the newly selected value.
    // So using this new value you could perform the desired processing
    // on your model here and return a view. Since this action was invoked
    // with an AJAX call you could return a JSON string to the client indicating
    // the success or failure of whatever processing you were intending to perform 
    // here

    ...

}

UPDATE:

If you wanted to submit the form containing the dropdowns when the selection changes you could use the following:

<script type="text/javascript">
    $(function() {
        // subscribe to the .change event of the dropdowns
        $('select').change(function() {
            // trigger the submission of the containing form:
            $(this).closest('form').submit();
        });
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文