I have a custom hidden field. The model uses a rendering parameter on the page to set the value of the hidden field, and the same form is used on multiple pages which is why I'm using a rendering parameter on the page to set the value instead of a field on the form field item. When I inspect the loaded page, I can see that value.
[Serializable]
public class CampaignIdFieldModel : InputViewModel<string>
{
public string CampaignId = "";
public string PlaceholderText = "";
public string FieldName = "";
protected override void InitItemProperties(Item item)
{
Assert.ArgumentNotNull(item, "item");
base.InitItemProperties(item);
FieldName = item.Name;
if (Sitecore.Context.PageMode.IsExperienceEditor || Sitecore.Context.Request.QueryString["sc_formmode"] != null)
{
PlaceholderText = "(Campaign Id)";
}
else if (!HttpContext.Current.Request.Url.ToString().Contains("form"))
{
var renderingRef = Sitecore.Context.Item?.Visualization.GetRenderings(Sitecore.Context.Device, false).FirstOrDefault(x => x.WebEditDisplayName == "Contact Us Form");
if (renderingRef == null) return;
var rendering = new Rendering
{
RenderingItemPath = renderingRef.RenderingID.ToString(),
Parameters = new RenderingParameters(renderingRef.Settings.Parameters),
DataSource = renderingRef.Settings.DataSource
};
var campaignId = rendering.Parameters["CampaignId"];
CampaignId = campaignId;
}
}
}
@using System.Web.Mvc.Html
@model VelirSite.Feature.Forms.Models.CampaignIdFieldModel
@Model.PlaceholderText
<input type="hidden" id="@Html.IdFor(m => Model.CampaignId)" name="@Html.NameFor(m => Model.CampaignId)" value="@Model.CampaignId" />
However, when the form submits, the campaignid field is there in the data but its value is null.
Notably, when debugging the code while submitting the form, it hits InitItemProperties
for CampaignIdFieldModel again and if I inspect this.Value
, it's null. So it seems like even though the field on the form has a value, when the form is submitted the field reinitializes and sets the value to null rather than passing in the value that's on the form. What am I doing wrong in my custom hidden field?
EDIT:
I have also tried putting this.Value = CampaignId;
in InitItemProperties
, it doesn't make a difference. When I submit the form, it reinitializes everything and both CampaignId and Value are reset to null