two submit buttons on a form

后端 未结 2 1693
天涯浪人
天涯浪人 2021-01-28 00:18

lets say I have a set of establishments, each establishments know who his father is and a establishment can have many childs. now I created a set of cascading dropdowns for this

相关标签:
2条回答
  • 2021-01-28 00:36

    Modify the Button like below.

    <input type="button" value="Select" class="btnSubmit" />
    

    Mofify the Form Tag as mentioned below

    @using (Ajax.BeginForm("SelectParent","Ticket", FormMethod.Post, 
                                                              new { id = "myForm" }))
    {
    }
    

    Modify the Div as mentioned below. Add an attribute which will have value corresponding to it's Controller's Action method.

    <div id="FirstHeritage" attr-Url="@Url.Action("ActionName", "ControllerName", 
    new { area = "AreaName" })"></div>
    

    Now in Jquery. Follow below steps.

    1. Load Partial View
    2. Fetch the Div Attribute Value
    3. Use On for the Button event.
    4. Ajax Request

    JQuery

    $(document).ready(function () {
        var FirstHeritage = $('#FirstHeritage');
        var url = FirstHeritage.attr('attr-Url');
        FirstHeritage.load(url, function () {
            var $form = $('#myForm');
            $.validator.unobtrusive.parse($form);
            $(document).on('click', '.btnSubmit', function () {
                if ($form.valid()) {
                    $.ajax({
                        url: Url,
                        async: true,
                        type: 'POST',
                        beforeSend: function (xhr, opts) {
    
                        },
                        contentType: 'application/json; charset=utf-8',
                        complete: function () { },
                        success: function (data) {
                            $form.html(data);
                            $form.removeData('validator');
                            $form.removeData('unobtrusiveValidation');
                            $.validator.unobtrusive.parse($form);
                        }
                    });
                }
            });
        });
    });
    

    Hope this will help you.

    0 讨论(0)
  • 2021-01-28 00:47

    You can't, essentially. The script that makes the AJAX form an AJAX form binds to the submit event, so any submit will be caught.

    Remember all the HTML helpers and controls in ASP.NET are there to cover common scenarios and make your life easier when you're actually in a common scenario. The more "custom" your code gets (such as a second submit button that will do a regular POST instead of an AJAX POST), the more work you need to do (and the less you should be using the builtin helpers and controls).

    Just create a regular form (Html.BeginForm), add your two submit buttons, and then attach a click event on the AJAX version, and then send the POST as AJAX yourself.

    0 讨论(0)
提交回复
热议问题