Unfortunately, there are several ways that you can integrate with Sitecore MVC and Sitecore, it does not contain many examples of best practice. For example, you can find it here .
In our projects, we do this a little differently, because we want to use as many agreements as possible, etc. from ASP.NET MVC by default. I am trying to include a complete simple example in this post.
We add two different hidden fields to the form with the current action and the current controller, the view is as follows:
@model Website.Models.TestViewModel @using (Html.BeginForm()) { @Html.LabelFor(model => model.Text) @Html.TextBoxFor(model => model.Text) <input type="submit" value="submit" /> <input type="hidden" name="fhController" value="TestController" /> <input type="hidden" name="fhAction" value="Index" /> }
With this simple ViewModel:
namespace Website.Models { public class TestViewModel { public string Text { get; set; } } }
Then we created a custom attribute that checks if the current controller / action matches the message:
public class ValidateFormHandler : ActionMethodSelectorAttribute { 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; } }
Then the controller actions receive a new attribute:
namespace Website.Controllers { public class TestController : Controller { public ActionResult Index() { return View(); } [HttpPost] [ValidateFormHandler] public ActionResult Index(TestViewModel model) { return View(model); } } }
We always return the view permitted by ASP.NET MVC. By convention, this view is with the same name as the action in the folder with the same name as the controller.
This approach works very well for us. If you want to add AntiForgeryToken , this also works great.