1

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" />

enter image description here

However, when the form submits, the campaignid field is there in the data but its value is null.

enter image description here

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

2
  • Please add to your question also how field is defined with your custom model Commented Jan 22 at 21:27
  • I'm not sure what you mean. Like how the Sitecore items are configured? Commented Jan 23 at 17:15

1 Answer 1

0

Value and CampaignId are different variables. 'Value' comes from the InputViewModel class and CampaignId comes from the custom model you created. When you inspect it on the Submit action you're looking into the 'Value' instead of 'CampaignId', but you're not populating the Value variable. You need to decide if you want to use Value or CampaignId, but in case Value is good, you should populate it such as:

protected override void InitItemProperties(Item item)
    {
        <!-- your code -->
        this.Value = campaignId;
    }

Don't forget to change the View (.cshtml) in case you switch to Value.

2
  • I previously tried including this.Value = campaignId; in the InitItemProperties, but on submit when it reloaded, value was null again Commented Jan 25 at 14:52
  • Ok. Seems that your data is missing somewhere. Try it: 1) Check the Field Item definition, you must have placed it somewhere at '/sitecore/system/Settings/Forms/Field Types/Basic/' and then check the template at '/sitecore/templates/System/Forms/Fields/' 2) Add the UpdateItemFields method to your code such as: protected override void UpdateItemFields(Item item) { <!-- maybe some code here --> item.Fields["Campaign Id"]?.SetValue(this.Value, true); } Commented Jan 26 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.