Multiple blocks with POST functionality on same page

Vote:
 

Hello,

I'm having two different blocks with regular form and submit button.

Each of them contains corresponding BlockController with GET / POST action exposed to render view and process submit action.

Is is possible to place both, different blocks on one page?

Currently even when I'm submitting second one, request is being routed/processed to first block controller.

What are steps required to correctly setup routing in above situation?

I have just sligtly modified Alloy MVC example to make it appear, what am I missing?

ContactBlockController.cs 

[HttpPost]
public virtual ActionResult Index(ContactBlockModel postedData, ContactBlock currentBlockLink, PageData currentPageLink)
{
     ...
}

PageListBlockController.cs

[HttpPost]
public virtual ActionResult Index(PageListModel postedData, PageListBlock currentBlockLink, PageData currentPageLink)
{
    ...
}

In each view model I have added single string property to be provided by user 

public string First/SecondValue { get; set; }

and in each view I have added form

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.SecondValue)
   
}

no changes to routes.

One solution that makes it work is to post to regular controller, but I wish to make use of already created block controllers.

#186722
Edited, Jan 03, 2018 12:06
Vote:
 

I think there are some issues in your code.

currentPage, currentBlock and currentContent are magic variable names that's used in a controller action to keep track of the currently viewed content. They are used in conjunction with PageController<T> and BlockController<T>. currentBlockLink and currentPageLink doesn't work in the same way - they are just regular variable names and therefore needs to passed from @Html.BeginForm(...).

However, you can't pass PageData or PageListBlock in BeginForm(...) since they are complex classes, you can send their content links though:

@Html.BeginForm(new { currentPageLink = page.ContentLink })

You're asking if it's possible to put two different blocks (that both expose GET and POST actions) on the same page. One thing that makes it difficult is that blocks don't have their own URL, like pages do. So, you can't post directly to a certain block but you could post to the block controller (which would act like a normal MVC controller). That means that you can't use currentPage, currentBlock and currentContent in your POST action method. To solve this, you need to manually pass data to the POST action the way I described earlier, plus information about the controller and action to execute:

@Html.BeginForm("Index", "ContactBlock", FormMethod.POST, new { currentPageLink = page.ContentLink, currentBlockLink = block.ContentLink })

I have not tested anything written above, but these are observations and solutions I've found myself.

#186731
Jan 03, 2018 13:27
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.