Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christopher Waters 7 posts 38 karma points
    Jul 29, 2015 @ 14:33
    Christopher Waters
    0

    Populate Umbraco Form Field from Querystring

    I'm working on upgrading a classic ASP website to Umbraco and bought a license for Umbraco Forms that I'm using on a few places around the site. I ran into a bit of an issue this morning when I was told that we needed to be able to pre-populate certain fields of a feedback form based on a querystring.

    We have thousands of computers around the world that are running our software that have links to the existing form pages with URL's like: "/feedback?product={product name}&version={version}&computer={computer name}" and being able to put those values into the form is vital for our support team to be able include those values in the email.

    Is there a way to specify the default value for fields from a querystring?

  • Casper Andersen 126 posts 508 karma points
    Jul 29, 2015 @ 18:39
    Casper Andersen
    0

    Yes there is.

    The way i hope you are doing this, is you have a partial view which contains your form,

    and then you have a page where you call a controller action like so

    @Html.Action("ActionName", "ControllerName")

    and inside the controller i hope you are istanciating a new model like so

    ModelName myModel = new ModelName();

    and then returning the partialview like so

    return PartialView("PartialViewName",myModel)

    IF THIS IS THE CASE

    you can then send along values when you call your controller action like this

    @Html.Action("ActionName", "ControllerName", new { id = Request.QueryString["key"],id2 = Request.QueryString["key"]})

    and in your controller you can then grab those variables and add them to your model so that the values are put into the form elements on the page

    Hope it makes sense, if not tell me and i will find another way to explain it

  • Christopher Waters 7 posts 38 karma points
    Jul 29, 2015 @ 19:38
    Christopher Waters
    0

    I was using Umbraco Forms to create/render the form, not creating a controller to do it. I was hoping I could set the default value in the editor to populate from the querystring instead of having to create a custom controller for it.

    I'm currently building out a RenderMvcController to take care of it now since there doesn't seem to be a built-in way to do it with Umbraco Forms and time isn't exactly on my side right now. I was using a SurfaceController originally, but found some undesirable functionality on form submission (losing data in case of error, etc.).

    One thing I have learned with using SurfaceControllers is that you don't need to explicitly pass values inside the @Html.Action call; ASP.NET's default model binder will pair querystring values to the appropriate method parameters (assuming the names match). That leads to cleaner code, IMO.

  • Nicholas Westby 1896 posts 6336 karma points c-trib
    Jul 29, 2015 @ 21:11
    Nicholas Westby
    0

    There was a way to do it with Contour, so presumably they would allow that with Umbraco Forms as well.

    There used to be a PDF you could download here: https://our.umbraco.org/projects/umbraco-pro/contour

    However, that PDF no longer appears.

    From what I remember, it was some special syntax you'd plug into the default for a field. Something like [#product] or [$product] or what have you. You might be able to find the particular syntax with some Googling.

    Again, not sure if Umbraco Forms kept this functionality.

  • Nicholas Westby 1896 posts 6336 karma points c-trib
    Jul 30, 2015 @ 17:58
    Nicholas Westby
    107

    By random chance, I came across the Contour syntax in this file: http://contourcontrib.codeplex.com/SourceControl/latest#Contour.Contrib/Contour.Contrib/FieldTypes/Label.cs

    [@queryParam]
    [%sessionValue]
    [^dictionaryItem]
    [#nodeProperty]
    [$nodePropertyRecursive]
    

    What you want in particular is [@queryParam]. Again, this is the Contour syntax. No idea if it works with Umbraco Forms. With the examples you gave, you would want:

    [@product]
    [@version]
    [@computer]
    

    Those would be the default values for your fields.

  • Ron Deijkers 2 posts 72 karma points
    Jul 05, 2018 @ 15:51
    Ron Deijkers
    0

    I just tried the [@queryParam] syntax and it seems to work correctly.

Please Sign in or register to post replies

Write your reply to:

Draft