2
votes

we have a Sitecore Project up and running, that is based on the regular aspx/ascx approach.

Over time we would like to transform our existing sublayouts to MVC.

For test purposes I am trying to add a very simple MVC text component to the project, still I am stuck somehow.

What I have done so far:

  • Installed MVC 5.2
  • Installed WebPages
  • Added references and bindings
  • Added the MVC Scaffold

Right now, the Site does compile and run.

I have this Controller:

    public class TextComponentController : Controller
    {
      public ActionResult Index()
      {
        return View();
      }
    }

And my view:

<h2>Index</h2>

<p>Hello from my View Template</p>

So absolutely nothing special here ;)

How can I create a sublayout (without a datasource) that just displays this simple MVC component?

1
Have you created a controller rendering in Sitecore?Fred
Not yet, but yes I have now seen that you can add a view rendering.Bgl86

1 Answers

7
votes

In the very simplest way, you need to have the following:

  1. Item for your page, the one that has URL; that is as normal in Sitecore
  2. That page Item should have Layout assigned. From Presentation --> Details menu select at least a layout on that stage. If you do not have layout yet, you need to create a layout definition item under /Layout/Layouts folder and associate it with certain *.cshml file. Also mention that layout should have a placeholder where you will "inject"your rendering.

    @Html.Sitecore().Placeholder("Main")
    
  3. You need to create a Controller Rendering under /Layout/Renderings folder in Sitecore. Make sure you set Controller and Controller Action fields to your controller name and action method name. enter image description here

  4. Finally, go again to Presentation --> Details --> Edit --> Controls and add your newly created rendering into a placeholder that you have on your layout *.cshtml file. enter image description here

That's all done.

Hope this helps!