James Harcourt
James Harcourt

Reputation: 6389

Multiple submits to different MVC controller actions from buttons or dropdownlist changes

If I have a single form - with two submits:

  1. From a save button - calls a form POST "Save" controller action.
  2. From a change of a dropdown list value - calls a form POST "NoSave" controller action that returns a modified view without saving.

What's the best way of achieving this?

At the moment, I have the following - but they both call the same POST controller action. I want to call a named action for the dropdownlist update.

<form form method="POST">

    <!-- dropdown list -->
    <div class="row">
            @Html.LabelFor(x => x.FieldName, "Field Name:")
            @Html.DropDownListFor(x => x.FieldName, Model.FieldName, new { @class = "browser-default", @onchange =  @"form.submit();" })
            @Html.ValidationMessageFor(x => x.FieldName)
        </div>
    </div>

    <!-- save button-->
    <div class="save-button">
        <input type="submit" class="btn" value="Save" />
    </div>
</form>

Upvotes: 1

Views: 287

Answers (2)

Rajan Mishra
Rajan Mishra

Reputation: 1178

For this you can use ajax.beginform in first parameter you have to give the name of action and then controller and then some option which are like method type and success and failure actions.

@using (Ajax.BeginForm("_LoadPartial", "Abss", new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccess", OnFailure = "OnFailure" }))
    {
    <div class="row">
                @Html.LabelFor(x => x.FieldName, "Field Name:")
                @Html.DropDownListFor(x => x.FieldName, Model.FieldName, new { @class = "browser-default", @onchange =  @"form.submit();" })
                @Html.ValidationMessageFor(x => x.FieldName)
            </div>
        </div>

        <!-- save button-->
        <div class="save-button">
            <input type="submit" class="btn" value="Save" />
        </div>
    }

Also provide OnSuccess and Failure Javascript fucntion on the same page.

<script>
    function OnSuccess(){
                        // some action
                        }

    function OnFailure(){
                        // some action
                        }
</script>

Upvotes: 0

moath naji
moath naji

Reputation: 661

what about using ajax request for different type of requests every type of request call different action or even different controller

[HttpPost]
        public ActionResult SomeFunction(string a)
        {
            return Json("some data here", JsonRequestBehavior.AllowGet);
        }


[HttpPost]
            public ActionResult AnotherSomeFunction(string a)
            {
                return Json("some data here", JsonRequestBehavior.AllowGet);
            }

//by click button

$("some button name ").click(function(){
    $.ajax({
            url: 'home/FirstAjax',
            success: function(responce){ alert(responce.data)},
            error: function(responce){ alert(responce.data)}
        });
 });

//by click another button

$("some button name ").click(function(){
    $.ajax({
            url: 'home/SecoundFirstAjax',
            success: function(responce){ alert(responce.data)},
            error: function(responce){ alert(responce.data)}
        });
 });

Upvotes: 1

Related Questions