I built a “CombineYourPurchase” block from where I need to post some products to the cart after clicking on a button.
I tried to use Html.BeginForm() method in my MVC view
using (Html.BeginForm("TestAction", "CombineYourPurchaseBlock"))
{
<div>
<button type="submit">@Html.WebSiteString("js.product.addtocart")</button>
</div>
}
}
in order to reach the post method in CombineYourPurchaseBlockController.cs, but I can not reach it. Instead I am being redirected to “/site.axd/CombineYourPurchaseBlock/TestAction”.
[HttpPost]
public ActionResult TestAction(BlockModel currentBlockModel, CombineYourPurchaseBlockViewModel vm)
{
return new RedirectResult("/GetTestResult/Index", true);
}
Is there another way I can access that post method in my block controller?
using (Html.BeginForm("TestAction", "CombineYourPurchaseBlock", FormMethod.Post))
we will instruct the ASP.NET Mvc framework to find the CombineYourPurchaseBlock-controller and post to the TestAction. If you instead send in null as the controller name the post will go to the current url instead.
Adding post actions into block-controller need to go directly to the controller and then a redirect need to be executed for the visitor, the reason is that you otherwise need to have the same action in the page-controller and in all other block-controllers, otherwise you will get the same exception as in Exception from block when POST is used where the method of POST not is found.
I set up a minimal example to test this locally and when posting from the block to a POST action on the block controller I hit my redirect back to the start page, as expected.
Have you made any changes to the routing config?
Can you hit the POST action if you strip the parameters?
Definition
using System;
using Litium.Accelerator.Constants;
using Litium.Blocks;
using Litium.FieldFramework;
using System.Collections.Generic;
namespace Litium.Accelerator.Definitions.Blocks
{
internal class CombineYourPurchaseBlockTemplateSetup : FieldTemplateSetup
{
private readonly CategoryService _categoryService;
public CombineYourPurchaseBlockTemplateSetup(CategoryService categoryService)
{
_categoryService = categoryService;
}
public override IEnumerable<FieldTemplate> GetTemplates()
{
var pageCategoryId = _categoryService.Get(BlockCategoryNameConstants.Pages)?.SystemId ?? Guid.Empty;
var templates = new List<FieldTemplate>
{
new BlockFieldTemplate("CombineYourPurchaseBlock")
{
CategorySystemId = pageCategoryId,
Icon = "fas fa-file-video",
FieldGroups = new []
{
new FieldTemplateFieldGroup()
{
Id = "General",
Collapsed = false,
Fields =
{
SystemFieldDefinitionConstants.Name,
}
}
}
}
};
return templates;
}
}
}
Controller
using Litium.Accelerator.ViewModels.Block;
using Litium.Runtime.AutoMapper;
using Litium.Web.Models.Blocks;
using System.Web.Mvc;
namespace Litium.Accelerator.Mvc.Controllers.Blocks
{
public class CombineYourPurchaseBlockController : ControllerBase
{
[HttpGet]
public ActionResult Index(BlockModel currentBlockModel)
{
return PartialView("~/Views/Block/CombineYourPurchaseBlock.cshtml");
}
[HttpPost]
public ActionResult Post()
{
return Redirect("/");
}
}
}