Home Changing mvc form controller on submit
Reply: 2

Changing mvc form controller on submit

Barr J
1#
Barr J Published in 2017-12-05 18:38:44Z

I recently came upon this question as I have a solution I need to work on.

I have a controller which does some logic, with some method:

   public Controller1: MvcController
   {
     public ActionResult SomeLogic(Model model)
     {
       return view();
     }
     public ActionResult SomeLogic2(Model model)
     {
       return view();
     }
  }

And I have a second controller which does different logic:

   public Controller2: MvcController
   {
     public ActionResult SomeLogic(Model model)
     {
       return view();
     }
     public ActionResult SomeLogic2(Model model)
     {
       return view();
     }
  }

Both are working on the same view:

MyView.cshtml

The view contains a begin form that works on submit:

@using(Html.BeginForm("SomeLogic", "Controller1", FormMethod.Post)

and 4 submit buttons:

<input type='submit' formaction='SomeLogic' value='submit' />
<input type='submit' formaction='SomeLogic2' value='submit2' />   
<input type='submit' formaction='SomeLogic' value='submit3' />
<input type='submit' formaction='SomeLogic2' value='submit4' />

I know I can control the action to which I am redirecting the submit using the: formaction attribute.

I there a way to change the controller as well? meaning that:

<input type='submit' formaction='SomeLogic' value='submit3' />
<input type='submit' formaction='SomeLogic2' value='submit4' />

Will submit to controller2 instead of Controller1 which is defined in my begin form.

This will solve my debate whether to create a new controller or use the existing one, which is already full of complex logic.

Stephen Muecke
2#
Stephen Muecke Reply to 2017-12-06 03:59:32Z

You need to include controller name in the formaction attribute.

Note that your controller names are not valid, but assuming your have

public FirstController: Controller
{
    [HttpPost]
    public ActionResult SomeLogic(Model model)
    {
        return view();
    }
}
public SecondController: Controller
{
    [HttpPost]
    public ActionResult SomeLogic(Model model)
    {
        return view();
    }
}

Then you can post to the correct controller using

// Post to the SomeLogic method of FirstController
<input type="submit" formaction="@Url.Action("SomeLogic", "First")" value="submit" />
// Post to the SomeLogic method of SecondController
<input type="submit" formaction="@Url.Action("SomeLogic", "Second")" value="submit3" />
Francis Ducharme
3#
Francis Ducharme Reply to 2017-12-06 14:38:00Z

Just intercept the submit with jQuery

You can add an ID to your form, to be able to select it easily in jQuery

@using(Html.BeginForm("SomeLogic", "Controller1", FormMethod.Post, new { @id = "yourForm" })

Then

$("#yourForm").submit(function(e){
    e.preventDefault();

    var url1 = '@(Url.Action("SomeLogic1", "SomeController", null, Request.Url.Scheme))';
    var url2 = '@(Url.Action("SomeLogic", "SomeController2", null, Request.Url.Scheme))';

    //Whatever logic should you do to select which URL you want to POST to

    $.ajax({
        type: 'POST',
        url: url_selected_above,
        data: $('#yourForm').serializeArray(),
        success: function (xhr, response) {
             //...
        },
        error: function (xhr, response) {
            //...
        }
    });
});

This should work if all the methods expect the same view model.

Alternatively, if you do not want to mess around in the UI, you can do something as such in the action that generates the form.

var controllerName = "SomeController";
var actionName = "Action1";

//do your logic to select whichever controller/action.

viewModel.ControllerName = controllerName;
viewModel.ActionName = actionName;

And in the view that has the form:

@using(Html.BeginForm(@Model.ActionName, @Model.ControllerName, FormMethod.Post)

Again, if all the actions expect the same view model, that should work.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331801 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO