Copied to clipboard

Flag this post as spam?

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


  • Conor Newman 6 posts 76 karma points
    Nov 01, 2023 @ 09:42
    Conor Newman
    0

    Error trying to get a page title into a form.

    I'm trying to help a friend with an existing Umbraco website. He has a template for a registration form for an event, on this template a hidden field called title populates with the page title (The registration event title)

    The following line pops that info into a variable.

     var titleInterval = interval.GetValue("title");
    

    This template is used by a Registration form in partialViews, the data collected across that form is then posted to a tracking system.

    My issue is when collecting the data to post I get an error here:

        <input type="text" name="Email" data-bind="value: Email">
        <input type="text" name="LastName" data-bind="value: LastName">
        <input type="text" name="FirstName" data-bind="value: FirstName">
        <input type="text" name="privacy" data-bind="value: AcceptTermsText">
        <input type="text" name="xxx-xxxx" data-bind="value: AcceptPermissonText">
        <input type="text" name="xxxxx_xxxx" data-bind="value: AcceptTandCText">
        <input type="hidden" name="titleInterval" data-bind="value: @(titleInterval)">
        <input type="submit" value="Sumbit" />
    </form>
    

    specifically on the line:

    The error states: 'The name titleInterval does not exist in the current context'.

    Any suggestions as to what I'm doing wrong? Is there an issue pulling the data from the template part of the page across to the form part? I can't get my head around why it won't work.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Nov 01, 2023 @ 16:06
    Marc Goodson
    1

    Hi Conor

    It sounds like what you are describing, and I may just be misunderstanding is that you have a template where you read in the variable

    var titleInterval = interval.GetValue("title");

    which successfully reads the value...

    ... but you have a form in a separate partial view??? (this is the bit I'm not sure of)

    that is rendered from the Template eg

    Html.Partial("mypartialview")

    and inside that you are trying to read the titleInterval value from the template?

    if so... then the PartialView is bound to the Model of the Template and any variables declared inside the template won't be automatically passed through to the partial view, and you should get an error if you tried along the lines of 'I don't know what titleInterval is mate'

    What you would need to do is pass in the titleInterval into the PartialView...

    If you don't specify a Model when you use Html.Partial then the model sent is the same model as the template.

    But if you want to pass extra information you can create your own ViewModel class

    and then use that to pass the data through to the Partial eg

    public class MyViewModel {
              public string TitleInterval {get;set;}
              public IPublishedContent CurrentPage {get;set;}
    }
    

    Then in your template you could create a new ViewModel

    var myViewModel = new MyViewModel(){ TitleInterval = titleInterval, CurrentPage = Umbraco.AssignedContentItem};

    (I'm assuming your PartialView needs other properties from the CurrentPage, if not no need to do this)

    then you would do

    Html.Partial("mypartialview",myViewModel);
    

    inside your partialview you'd need to change the @model or @inherits at the top to be

    @inherits UmbracoViewPage<MyViewModel>
    

    and then you could write

      <input type="hidden" name="titleInterval" data-bind="value: @(Model.TitleInterval)">
    

    ...

    but if I have misconstrued and the form fields are in the same template as your titleInterval variable, then check if it's declared inside an @if or something eg

    @if (something) {
    var titleInterval = "fish";
    }
    
    <h2>@titleInterval</h2>
    

    would error because titleInterval is only declared inside the scope of the if...

    ... so that catches me out all the time...

    and the workaround is to declare it outside the if eg

    var titleInterval = "";
    @if (something) {
    titleInterval = "fish":
    }
    

    then it would be ok...

    but sorry if I've misunderstood the issue!

    regareds

    marc

Please Sign in or register to post replies

Write your reply to:

Draft