Hi Kevin
It can be a little tricky to get form posts right with Episerver content.
To post the form to the URL of the page (with AddToCart appended), you may need to remove the controller name from you BeginForm method call. Maybe you will also need to add the content link your route values.
Something like this:
@using (Html.BeginForm("AddToCart", null, new { area = "commercearea", node = Mode.Variation.ContentLink }, FormMethod.Post ))
Let me know how it works out.
Hi Stepan,
Thank you for the help but the currentConent variable is still null. public ActionResult AddToCart(TestProduct currentContent, string session, string code)
The TestProduct won't bind during the call.
No the URL hasn't changed. I made the changes you suggested above but the currentContent isn't instantiating.
Hi Kevin,
Could you share the complete controller code instead of just sharing the methods?
Hi Sanjay,
namespace Cms.Web.CommerceArea.TestProduct.Controllers
{
public class TestProductController : ContentController<Cms.Web.CommerceArea.TestProduct.Models.TestProduct>
{
private readonly TestProductEntryViewModelFactory _viewModelFactory;
public TestProductController(TestProductEntryViewModelFactory viewModelFactory)
{
_viewModelFactory = viewModelFactory;
}
[HttpGet]
public ActionResult Index(Cms.Web.CommerceArea.TestProduct.Models.TestProduct currentContent, string entryCode = "", int count = 0, string sessionKey = "")
{
var viewModel = _viewModelFactory.Create(currentContent, entryCode);
viewModel.ItemCount += count;
viewModel.Session = sessionKey == "" ? Guid.NewGuid().ToString() : sessionKey; // TEMP - Until session is put in place
return View(viewModel);
}
[HttpPost]
public ActionResult AddToCart(Cms.Web.CommerceArea.TestProduct.Models.TestProduct currentContent, string session, string code, string itemGuid)
{
_viewModelFactory.AddToCart(currentContent, session, itemGuid);
return RedirectToAction("Index", new { currentContent = currentContent, entryCode = code, count = 1, sessionKey = session });
}
}
}
Could you check the Cms.Web.CommerceArea.TestProduct.Models.TestProduct is inherited from ProductContent content type such as ?
Cms.Web.CommerceArea.TestProduct.Models.TestProduct : ProductContent
Hi,
It is a quite old post, but do not know if you ever found the problem? We have seen this issue when your Post action is not using MVC HttpPost. Please ensure you are using [System.Web.Mvc.HttpPost] not [System.Web.Http.HttpPost].
~ Sujit
I'm posting a form to a regular controller and all of the properties are populated except for the currentContext variable. The currentContext varialble contains the product for the cms page. I'm new to EpiServer and I'm not sure if I'm descrbing this correctly.
When the Index action is called the currentContext is populated and the page is displayed fine. But when I post to the AddToCart method the currentContext is null.
Please see the code below:
TestProductController.cs
[HttpGet]
public ActionResult Index(TestProduct currentContent, string entryCode = "", int count = 0, string sessionKey = "")
{
var viewModel = _viewModelFactory.Create(currentContent, entryCode);
viewModel.ItemCount += count;
viewModel.Session = sessionKey == "" ? Guid.NewGuid().ToString() : sessionKey;
return View(viewModel);
}
[HttpPost]
public ActionResult AddToCart(TestProduct currentContent, string session, string code)
{
var pageRouteHelper = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<EPiServer.Web.Routing.IPageRouteHelper>();
var page = pageRouteHelper.Page;
_viewModelFactory.AddToCart(currentContent, session, code);
return RedirectToAction("Index", new { entryCode = code, count = 1, sessionKey = session });
}
Index.cshtml
@model Cms.Web.CommerceArea.TestProduct.TestProductViewModel
@using (Html.BeginForm("AddToCart", "TestProduct", new { area = "commercearea" }, FormMethod.Post ))
{
@Html.HiddenFor(x => x.Session)
@Html.Hidden("code", Model.Variation.Code)
<div>
Product Name:@Html.PropertyFor(x => x.Product.Name)
</div>
<div>
Variation Name: @Html.PropertyFor(x => x.Variation.Name)
</div>
<div>
Sku: @Html.PropertyFor(x => x.Variation.Code)
</div>
<div>
Cart Count: @Html.PropertyFor(x => x.ItemCount)
</div>
<div>
<button type="submit" role="button">Add to Cart</button>
</div>
}
Any help would be greatly appreciated.
Thank you