8

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?

  • Are you using the Controller or SitecoreController as base class to your controller, and how have you defined your controller rendering in Sitecore? – Søren Engel Oct 28 '16 at 13:02
  • Just a small note - The last behavior you are mentioning, is explained in Martina's blogpost you've linked to. – Søren Engel Oct 28 '16 at 13:04
  • I am using controller as base class. I have created a controller rendering with controller name and action Same as for the get – Danny Oct 28 '16 at 13:16
  • Okay, so you have both a HttpGet and HttpPost action in your controller named the same as defined in the controller rendering? – Søren Engel Oct 28 '16 at 13:18
  • I added my controller code – Danny Oct 28 '16 at 13:22
5

I'd say you can either use the first or second approach.

I'm normally using the first approach (the one you got working now) for these sorts of things. What I do is having the same method action defined twice, one for get and one for the post, and in the post version I pass in the ViewModel, which I then rebuild the same way you are doing it. The action methods should be annotated correctly with the [HttpGet] and [HttpPost] attributes.

If you use Html.BeginForm(controller, controllerAction), you will experience that you are being redirected to the controller's URL (like /sitecore/api/{controller}/{action}), and not the page containing the controller rendering(s), as you'd expect. This is something to keep in mind when doing some sort of model checking, where you want to show the error message in the view, if the model is not valid.

  • Is it normal behaviour that in the second option my Current rendering context is null? – Danny Oct 30 '16 at 18:09
  • I'm not actually sure about whether or not it's default behaviour in Sitecore's MVC implementation, but I would say it's not default ASP.NET MVC implementation behaviour. Remember that Sitecore MVC is an implementation of ASP.NET MVC, so there are a few key differences. I've noticed this behaviour myself when using the second option, but for some reason the SiteContext.Item is not null, even though the RenderingContext is. – Søren Engel Nov 1 '16 at 13:35
3

The first option is the one I have used mostly. But for some form submissions we wanted to have the page context as we had to pull data source of some other rendering on the page. So, my two cents would be

  • Check if you need page context, if so, pass in the page GUID to be handy
  • If possible use MVC Ajax posts to restrict full page reload and exchange only what is required. Even here you can pass the page GUID.
  • So always use the first option is the best approach? In that situation I have the page and rendering context – Danny Oct 30 '16 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.