You will need to have the following data templates:
Pizza Size Container
This template will have no fields. It will only have insert options to add
pizza sizes
Pizza Size
This template will contain one field section 'Size Details' and one
field of type Single line text
called Size
In your content tree, you will have to create the Pizza Size Container, and inside it, all the different sizes that you will need.
Next step is to create a Controller Rendering.
In the controller rendering, you will need to get to the Pizza Size Container found in your content tree using either the path of the item or item id of Pizza Size Container.
Suppose the path is sitecore\Content\xxx\Shared\Pizza Sizes
.
You can get the different pizza sizes using the code below.
var pizzaContainer = Sitecore.Context.Database.GetItem("sitecore/Content/xxx/Shared/Pizza Sizes");
var listOfSize = new List<PizzaSize>();
if (pizzaContainer != null)
{
var pizzaSizes = pizzaContainer.Children.ToList();
if (pizzaSizes.Any())
{
foreach (var pizzaSize in pizzaSizes)
{
listOfSize.Add(new PizzaSize
{
SizeId = pizzaSize.ID.ToString(),
SizeName = pizzaSize.Fields["Size"].Value
});
}
}
}
You need to have a partial view with model List<PizzaSize>
.
This is a snippet of the view.
Notice the Html.BeginForm
where the first parameter is the method name and the second one is the controller name.
@model List<PizzaSize>
@using (Html.BeginForm("PostPizzaSize", "Pizza", FormMethod.Post,
new { @role = "form" }))
{
if (Model != null && Model.Any())
{
<p>Select a Pizza Size</p> <!--Use a resource field to store this text in Sitecore-->
foreach (var entry in Model)
{
@Html.RadioButton("pizzaSize", entry.SizeID) @entry.SizeName
}
<button type="submit">Submit</button>
}
}
When submitting the form, the method PostPizzaSize
will be called and the form collection will be passed as parameter.
[System.Web.Http.HttpPost]
public ActionResult PostPizzaSize(FormCollection form)
{
var size= form["pizzaSize"]; //the value will be the item id of the size
}