I am trying to get a form in my MVC Controller Rendering to work. I have read these posts:
https://mhwelander.net/2014/05/30/posting-forms-in-sitecore-mvc-part-2-controller-renderings/
https://ctor.io/posting-forms-in-sitecore-controller-renderings-another-perspective/
I have tried these form options
My controller
public class VacanciesController : SitecoreController
{
public ActionResult VacancyDetail()
{
var model = RenderingContext.Current.Rendering.GetRenderingModel<VacancyDetailModel>();
var item = Sitecore.Context.Item;
return this.View(model);
}
}
with this attribute
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
var controller = controllerContext.HttpContext.Request.Form["fhController"];
var action = controllerContext.HttpContext.Request.Form["fhAction"];
return !string.IsNullOrWhiteSpace(controller)
&& !string.IsNullOrWhiteSpace(action)
&& controller == controllerContext.Controller.GetType().Name
&& methodInfo.Name == action;
}
this is my view form code
@using (Html.BeginForm())
{
<button id="btn-save" class="btn btn-highlight" type="submit">Solliciteer direct empty form</button>
<input type="hidden" name="fhController" value="Vacancies" />
<input type="hidden" name="fhAction" value="VacancyDetail" />
}
This seems to work now
@using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post))
{
@Html.Sitecore().FormHandler("Vacancies", "VacancyDetail")
<button id="btn-save" class="btn btn-highlight" type="submit">Solliciteer direct routeform</button>
}
this goes to the correct controller and action. Bu my RenderingContext.Current.Rendering is null
@using (Html.BeginForm("VacancyDetail", "Vacancies", new { id = "1" }, FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
{
<button id="btn-save" class="btn btn-highlight" type="submit">Solliciteer direct routeform</button>
}
This goes to the correct controller and action. But Sitecore.Contect.item is null & RenderingContext.Current.Rendering is null
What is the correct way to create a custom form?
Controller
orSitecoreController
as base class to your controller, and how have you defined your controller rendering in Sitecore?