Sitecore multiple forms on one page - view renderings

I was trying to read out form values after a POST and was reminded by a colleague that all controller actions will be able to render the form. That simply will not do. He referred me to some excellent blog posts which offer a solution for the controller renderings. Check out Reinoud Van Dalen’s ultimate fix for multiple forms on one page with sitecore mvc

However I happened to have a view rendering on the receiving end of the form post. This blog post will explain how you can apply the same principle to view rendering.

Create a sitecoreHelperExtension

This code was copied form Reinoud’s solution, with a small modification to allow the developer to choose his own id (in case you want to use the id from some kind of generated code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static MvcHtmlString RenderingToken(this SitecoreHelper helper, string id = null)
{

if (helper.CurrentRendering == null)
{
return null;
}

var tagBuilder = new TagBuilder("input");
tagBuilder.Attributes["type"] = "hidden";
tagBuilder.Attributes["name"] = "uid";
tagBuilder.Attributes["value"] = id ?? helper.CurrentRendering.UniqueId.ToString();

return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing));
}

Update your form

You can call this without a parameter and it will use the id of the current rendering (default).

1
@Html.Sitecore().RenderingToken()

Alternatively you could call it using a custom Id, in this case the item Id from our generated code

1
@Html.Sitecore().RenderingToken(ItemTree.Layout.Renderings.MyRendering.ItemID.ToGuid().ToString())

This will generate a hidden field with the unique id of the current rendering (or the ID of your choice). That value will then get posted with the rest of you form.

remark the ToGuid() method will remove the curly braces. You can skip that if you don’t mind them in you values. Just don’t forget to use the same structure (with or without curly braces) when reading it back out.

Creating a form Extension

The first method checks for the correct source form. It does this by comparing the posted Id with the Id you provide it in the renderId parameter. The second method reads the form value in case the source is confirmed to be the correct form.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static class FormExtensions
{
public static bool IsSourceForm(this HttpRequestBase request, string renderId, string sourceField = null)
{

string sourceFieldName = sourceField ?? "uid";
return request.Form != null && !request.Form[sourceFieldName].IsNullOrEmpty() &&
request.Form[sourceFieldName].Equals(renderId);
}

public static string GetFormSourceValue(this HttpRequestBase request, string renderId,
string formFieldName, string sourceFieldName = null)

{

if (!IsSourceForm(request, renderId, sourceFieldName))
{
return null;
}

return request.Form[formFieldName].HasValue()
? request.Form[formFieldName]
: string.Empty;
}
}

Here’s the one liner on how to get the value you want, from the form you want .

1
2
var formID = ItemTree.Layout.Renderings.MyRendering.ItemID.ToGuid().ToString();
var formValue = Request.GetFormSourceValue(formID, "name of form value");