简体   繁体   English

在一个表单中通过下拉菜单多次提交

[英]Multiple Submit from Drop downs in a single Form

I have a MVC5 setup with two Dropdowns that via Javascript automatically submits when a value is selected. 我有一个带有两个下拉菜单的MVC5设置,当选择一个值时,这些下拉菜单会通过Javascript自动提交。 They are currently inside the same form, so i would like to have them submit to different Actions on my Backend 它们目前处于同一表格中,所以我希望它们在我的后端上提交给不同的操作

View: 视图:

<div class="panel-body">
    @using (Html.BeginForm("", "BrugerSession"))
    {
        @Html.AntiForgeryToken()
        <div class="row">
                <div class="col-md-6">
                @Html.LabelFor(model => model.Emails)
                @Html.DropDownListFor(x => x.ValgtEmail, Model.Emails, "Vælg Email")
            </div>
            <div class="col-md-6">
                @Html.LabelFor(model => model.Printere)
                @Html.DropDownListFor(x => x.ValgtPrinter, Model.Printere)
            </div>
        </div>
    }
</div>

JavaScript 的JavaScript

$(document).ready(function () {
    $("#ValgtEmail").change(function () {
        $(this).closest('form').trigger('submit');
    });
});

$(document).ready(function () {
    $("#ValgtPrinter").change(function () {
        $(this).closest('form').trigger('submit');
    });
});

The trick here is, that i am using How do you handle multiple submit buttons in ASP.NET MVC Framework? 这里的诀窍是,我正在使用如何处理ASP.NET MVC Framework中的多个提交按钮? to support multiple submit-targets in the Backend. 以支持后端中的多个提交目标。

Why main Question is: Can the Javascript Trigger method submit the data in the propper way, so it will works with the Solution from the above link? 为什么要提出的主要问题是:Javascript触发方法能否以适当的方式提交数据,因此它将与上述链接中的解决方案一起使用?

I tried looking into the http://api.jquery.com/trigger/ Documentation and there is support for additional parameters. 我尝试查看http://api.jquery.com/trigger/文档,并支持其他参数。 But i do know how to format my Javascript to achieve what I need. 但是我确实知道如何格式化我的Javascript以实现我所需要的。

Update: 更新:

I never managed to get this working. 我从来没有设法使这个工作。 Instead i surrounded each Select with its own form. 取而代之的是,我用自己的形式将每个Select包围起来。

I hope my answer will help you. 希望我的回答对您有所帮助。 You can use JQuery AJAX. 您可以使用JQuery AJAX。

First drop down 首先下拉

$("#dropdown1").change(function(){
    $.ajax({
        url: '/ControllerName/Action1',
        data: { parm1:parm1 },
        success: function () {
         // Success function
        },
        error: function () {
         // Error message
        }
    });
});

Second drop down 第二次下拉

$("#dropdown2").change(function(){
    $.ajax({
        url: '/ControllerName/Action2',
        data: { parm1:parm1 },
        success: function () {
         // Success function
        },
        error: function () {
         // Error message
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM