使用Jquery时无法处理多个提交按钮.submit() - MVC 5

时间:2016-08-28 09:30:46

标签: jquery asp.net-mvc

在此之前一切都运转良好。我可以在一个动作中处理多个按钮。

这是我的代码段:

HTML

@using (Html.BeginForm("AssignPlan", "Accounts", FormMethod.Post, new { @id= "formAssign", @class = "form-horizontal" }))
{
    //Input
    <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-primary" name="Command" value="Submit"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Assign Plan Only</button>
        <button type="submit" class="btn btn-primary" name="Command" value="Approved"><span class="fa fa-check"></span> Assign Plan With Approved</button>
    </div>
}

行动

[HttpPost]      
public ActionResult AssignPlan(AssignPlanModel M, string Command)
{
    // my logic
}

然后我决定使用ladda加载按钮,然后问题就开始了。我的字符串Command始终会在我的操作中返回NULL

这里是修改后的代码

HTML

@using (Html.BeginForm("AssignPlan", "Accounts", FormMethod.Post, new { @id= "formAssign", @class = "form-horizontal" }))
{
    // Input
    <div class="col-sm-offset-2 col-sm-10">
        <button type="submit"class="ladda-button btn btn-primary" data-style="zoom-in" name="Command" value="Submit"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Assign Plan Only</button>
        <button type="submit" class="ladda-button btn btn-primary" data-style="zoom-in" name="Command" value="Approved"><span class="fa fa-check"></span> Assign Plan With Approved</button>
    </div>
}

我添加了一些jQuery脚本

<script type="text/javascript">
$(document).ready(function () {
    $('#formAssign').submit(function () {
        if ($(this).valid()) {
            var val = document.activeElement.getAttribute('value');

            if(val == 'Approved')
            {                   
                var l = $('#BtnApp').ladda();

                l.ladda('start');

                $("#BtnSubmit").attr("disabled", true);
            }
            else
            {         
                var l = $('#BtnSubmit').ladda();

                l.ladda('start');

                $("#BtnApp").attr("disabled", true);
            }
        }
    })
});
</script>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

最后我在jQuery中添加了新的输入和修改过的脚本,并且正在运行。

<script type="text/javascript">

    $(document).ready(function () {

        $("#BtnSubmit").click(function(){

            var input = $("<input>")
               .attr("type", "hidden")
               .attr("name", "Command").val("Submit");

            $('#formAssign').append($(input));


            if ($('#formAssign').valid()) {

                var l = $('#BtnSubmit').ladda();

                l.ladda('start');

                $("#BtnApp").attr("disabled", true);

                $('#formAssign').submit();
            }
        });

        $("#BtnApp").click(function(){

            var input = $("<input>")
                        .attr("type", "hidden")
                        .attr("name", "Command").val("Approved");
            $('#formAssign').append($(input));

            if ($('#formAssign').valid()) {



                var l = $('#BtnApp').ladda();

                l.ladda('start');

                $("#BtnSubmit").attr("disabled", true);

                $('#formAssign').submit();
            }
        });
    });
</script>