0

I cannot work out why BeginUmbracoForm is generating a form that posts back to the wrong controller. I figure this is due to data within the hidden upfrt field but I cannot find a solution or really understand why it's happening.

  1. MainFormController (a RenderController) returns a view MyMainView
  2. MyMainView loads MyViewComponent
  3. MyViewComponent returns a view MyFormView
  4. MyFormView uses Html.BeginUmbracoForm - I want this to post back to SecondaryFormController (a SurfaceController)

The form always posts back to MainFormController, why?

What’s happening that triggers Umbraco to route to MainFormController rather than SecondaryFormController?

Here’s trimmed down version of the code:

MainFormController:

public class MainFormController: RenderController {
 public MainFormController() {}
 public override IActionResult Index() {
 var viewModel = new MainFormControllerViewModel() {
 ...
 }
 return CurrentTemplate(viewModel); // MyMainView
 }
}

MyMainView for MainFormController.Index:

@inherits UmbracoViewPage <MyApp.MainFormControllerViewModel>
@(await Component.InvokeAsync < MyApp.MyViewComponent > ())

MyViewComponent:

public class MyViewComponent: ViewComponent {
 public MyViewComponent() {
 }
 public async Task <IViewComponentResult> InvokeAsync() {
 return View(new MyViewComponentViewModel())
 }
}

The view/template for MyViewComponent:

@model MyApp.MyViewComponentViewModel
@using(Html.BeginUmbracoForm < MyApp.SecondaryFormController > (
 nameof(MyApp.SecondaryFormController.MyAction),
 "Update",
 null,
 ) {
 <button type ="submit">Update</button>
 }

SecondaryFormController.MyAction:

 public class SecondaryFormController: SurfaceController {
 public SecondaryFormController() {}
 [HttpPost]
 [ValidateAntiForgeryToken]
 [UnsupportedOSPlatform("browser")]
 public async Task <IActionResult> MyAction(MyViewComponentViewModel model) {
 // do something
 return CurrentUmbracoPage();
 }
 }

If I drop UmbracoBeginForm and use a normal form, it will post to SecondaryFormController.MyAction but I lose the Umbraco context which is no good.

If I drop the view component, and render the form on first view (MyMainView) the behaviour remains the same.

What am I missing here?

asked Apr 22, 2025 at 18:01

1 Answer 1

0

Detail matters.

In an attempt to keep this brief, I glossed over the fact MyFormView contains another view component. This is fine, but it happens to be a form, and it's rendering before `@using(Html.BeginUmbracoForm <MyApp.SecondaryFormController ...` is closed.

This results in a form within a form which is invalid HTML and just breaks things.

Refactoring so the form isn't rendered within another form fixes the issue.

answered Apr 23, 2025 at 11:46
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.