Custom Form Piranha CMS does not render in "Content" area

207 views Asked by At

I am trying to implement an custom form in Piranha CMS 2.2.4. The form renders to a new page, showing only the form. My goal is the that the form renders juist like a normal content page.

My code snippets: Index.cmshtml

@inherits Piranha.WebPages.SinglePage<Piranha.Models.PageModel>
@{
  var msg = "";

  if (IsPost)
  {
    var order = new Order()
    {
      Item = Request["Item"],
      Quantity = Request["Quantity"]
    };
    order.Save();
    msg = "Your order is saved!";
  }
}

@if (!String.IsNullOrEmpty(msg))
{
  <p>@msg</p>
}
<div>
  <form method="post">
    <input name="Item" />
    <input name="Quantity" />
    <button type="submit">Let's buy it!</button>
  </form>
</div> 

Order.cs:

public class Order
{
  public String Item { get; set; }
  public String Quantity { get; set; }

  public Order()
  {
  }

  public void Save()
  {
  }
}

What I am missing?

Any help is appreciated

2

There are 2 answers

0
Håkan Edling On

I don't think the default template has a _PageStart.cshtml, so unless you've added one manually specifying the Layout you need to specify it in your page.

Regards

Håkan

0
user4393443 On

You saved my day!

I modified the form and it works. I am pretty new to MVC and I am sure there are cleaner ways to achieve the same result. But for now it works. Thanks!

New code: @inherits Piranha.WebPages.SinglePage

@{
  //this is the main 'trick'
  Layout = "~/Content/_Layout.cshtml";
  Page.Title = Model.Page.Title;
}

<div>
  <form method="post">
    <input name="Item" />
    <input name="Quantity" />
    <button type="submit">Let's buy it!</button>
  </form>
</div>

<div class="content">
  <div class="main">
    <h4>@Model.Page.Title</h4>
    //@Model.Regions.Content

    @{
      var msg = "";

      if (IsPost)
      {
        var order = new Order()
        {
          Item = Request["Item"],
          Quantity = Request["Quantity"]
        };
        order.Save();
        msg = "Your order is saved!";
      }
    }

    @if (!String.IsNullOrEmpty(msg))
    {
      <p>@msg</p>
    }

  </div>
  <div class="sidebar">
    @RenderPage("~/Shared/Sidebar.cshtml")
  </div>
</div>