6

I'm using Areas in my Sitecore MVC solution. I THINK I want to post to my form back to the same controller just like a typical MVC project (having a HttpPost attribute in my controller). Maybe that's not the best practice with Sitecore, I don't know.

I've found how to do this without areas, but not with. Can this be done with Areas? Or why should I not do with with Areas?

1 Answer 1

6

Well I must admit before attempting to answer this question, I've never tried to build a form within a separate Area, but I thought it would be easy, so I went ahead and created my own test in my local sandbox environment. Turns out, it was relatively straight forward, but you might run into issues once you start putting this form on more complex pages.

So I started out with a basic MVC project. My environment is 8.2 update 3 with Mvc 5.2.3. So if you are in a much older version of Sitecore, I could see how this might be more challenging. I know support for Areas, was only built in, with more recent versions of Sitecore.

In that basic MVC project, I created an area called Site. Once it was done scaffolding, I created a new FormController and defined the following code in that Controller:

namespace Example.Web.Areas.Site.Controllers 
{
    public class FormController : GlassController
    {
        public ActionResult Submit()
        {
            var model = new SubmitModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Submit(SubmitModel model)
        {
            if (ModelState.IsValid)
            {
                Response.Redirect("/");
            }
            return View(model);
        }
    }
}

So really quick explanation, is that I have to action results with the same name. The one without the http verb attribute, will be get by default. Then I specify HttpPost for the submit action, which will run on the click of the submit button.

My model for this, just looks like the follow. So nothing too crazy, and I'm skipping the handling of validation, but you should probably add in atleast e-mail validation.

namespace Example.Web.Areas.Site.Models
{
    public class SubmitModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
}

Lastly I define the view with the code below. Very straight forward, but the difference is that I'm triggering the entire page to post back, which is typically what you'd want to do. You would just use the Sitecore Mvc Route here instead and it'll take care of everything for you.

@using Example.Web.Areas.Site.Models
@using Sitecore.Mvc
@using Glass.Mapper.Sc.Web.Mvc

@model SubmitModel

@using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post))
{

    @Html.Sitecore().FormHandler()
    <div class="form-control">
        @Html.LabelFor(x => x.FirstName)
        @Html.TextBoxFor(x => x.FirstName)
    </div>
    <div class="form-control">
        @Html.LabelFor(x => x.LastName)
        @Html.TextBoxFor(x => x.LastName)
    </div>
    <div class="form-control">
        @Html.LabelFor(x => x.Email)
        @Html.TextBoxFor(x => x.Email)
    </div>

    <button type="submit">Submit</button>
}

So basically my Website structure looks like this:

  • App_Config
  • Areas
    • Site
      • Controllers
        • FormController.cs
      • Models
        • SubmitModel.cs
      • Views
        • Form
          • Submit.cshtml
  • Controllers
  • Models
  • Views

Once all of that is setup, you'll need to go into Sitecore. Go to Sitecore/Layouts and open up Renderings. Create a new Controller Rendering and make sure to set it's Controller, Controller Action and Area correctly to match the controller Action Submit that we created early.

That is it, now you should be able to assign this rendering to a view, and publish your changes to start seeing your form. Like I said this is a fairly quick setup, but should get you running with making a more advanced page.

Also just to clarify, if you are doing multi-tenancy, than yes Areas is a good approach to achieve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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