Get the query string value in Kentico 12SP Form text input field.

Yashdeep Patidar asked on May 20, 2020 19:01

Hi, I am using Kentico forms to create a form. I need to use the value of the query string in one of the text input in the form which will be hidden and When user submits the form, the value of the hidden field should also be submitted with the form.

Currently I am not able to bind the value of query string in my form field. I have tried to put the value of the query string in the default value parameter of the form field by using the macro expressions like {% QueryString.userId|(identity)GlobalAdministrator%} and {% userId %}. Both of these macro expressions are not working for me.

Also when I try to hide the form field by changing the visibility condition of the field to "Never" the field value don't come when user submits the form. But I want the hidden field default value also when user submits the form.

Now I am trying to hide the form field using CSS and also trying to bind the value of query string in the hidden form field using jQuery, once the form is loaded. So that I will get the value of hidden field also when user submits the form.

Is there any other approach which I can follow to get the query string value in the hidden form field and the hidden field value should also be submitted when user submits the form.

I am using Kentico 12SP version to create the form and my live site is built using MVC development model.

Correct Answer

Dmitry Bastron answered on May 20, 2020 23:46

Hi Yashdeep,

I believe what you are trying to do is easier to achieve with custom MVC Form Component. Have a look in the documentation and example.

If your form control properties will be something like:

public class CustomFormComponent : FormComponent<CustomFormComponentProperties, string>
{
    [BindableProperty]
    public string HiddenValue { get; set; }

    public override string GetValue()
    {
        return HiddenValue;
    }

    public override void SetValue(string value)
    {
        if (!string.IsNullOrEmpty(value))
        {
            HiddenValue = value;
        }
        else
        {
            SetValue(Request.QueryString["QueryStringParamName"]);
        }
    }
}

And in view something like:

@model CustomFormComponent
@Html.HiddenFor(m => m.HiddenValue)

I think this should work.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Yashdeep Patidar answered on May 22, 2020 07:16

Thank you Dmitry, its working now.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.