Watch video

Building a surface controller

The very first step is to add a new folder to hold to the surface controller - and potentially other controllers that you might be adding to the project in the future.

Make sure that you create an Empty MVC 5 Controller, in order to ensure compatibility with the premade project.

Naming your controller

'Controller' is part of the MVC framework, and an important part of that. It is important that you prefix the name of your controller with 'Controller' in order for the file to be identified as an actual MVC controller.

Once you've created the controller, you will notice that it by default is set up to inherit from a Controller class that comes from System.Web.MVC. Make sure to update this, so it instead inherits from the SurfaceController class from Umbraco.Web.MVC - as we ARE creating a Umbraco project and not a standard MVC project.

The two methods

In this video, you will be setting up two different methods in your surface controller. One is to render out the Partial View created for the form in the previous video, and one is for handling the data submitted by the form.

Render() Method:

This method will be used to render out the Partial View with the form that you set up in the previous video. The method will also ensure that when the Partial View is then rendered out, that it's already mapping the properties from the model to the fields on the form.

Submit() method:

This method is the one that we will continue working on throughout this chapter. Eventually, this method will hold all the code that ensures that the data being submitted via the form on the website will get mapped to the correct properties on the model and again to the correct properties on the Document Type in the Umbraco backoffice. It is the most essential method in this chapter, as it is this method that creates our content and media and publishes it on the website - with a little help from some Umbraco Services, but more on that in the next chapter.




Check out the resources below to learn more about MVC and MVC in Umbraco.

Project login details

Password: 1234567890

Surface Controllers

Total Time: 01:18:05

In the Surface Controllers chapter, you will learn more about some of the key functionality in Umbraco and how you can use MVC to programmatically add content and media to your Umbraco website.