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:
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.
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
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
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.
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:
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.
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
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
inside your partialview you'd need to change the @model or @inherits at the top to be
and then you could write
...
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
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
then it would be ok...
but sorry if I've misunderstood the issue!
regareds
marc
is working on a reply...