Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hello:
I have a foreach loop that I have some javascript in. It does not recognize the javascript and script, but as razor code. How do I denote that the chunk is javascript?
@foreach(var contact in Model.inquiryContacts) { if(selectedOption == @contact.inquiry){ alert(@contact.inquiry); } }
Any help would be much appreciated.
Thank you.
The <text> tags doesn't seem to work in this case.
You could use @Html.Raw:
var jsAlertFormat = "alert({0});";var jsAlert = string.Format(jsAlertFormat, contact.inquiry);
@Html.Raw(jsAlert)
I don't see, but trust that you are in, a <script> context.
What about the if condition? Should I put that into a string too?
Yes it is all inside <script> tags
No, your condition is part of your code, which isn't the problem - leave that as is and extend it with what I provided.
aaagghhh it's not working
Got it !
@foreach(var contact in Model.inquiryContacts) { @Html.Raw( "if(selectedOption == '" + @contact.inquiry.InnerText + "'){emailChangeTo = '" + @contact.email.InnerText + "';} \r\n" ); }
For some reason, the String.Format wouldn't work....but yea...HTML.Raw is the way to go !!!! Thanks so much !!!!
Oh, I didn't realise the 'if' statement was supposed to be part of the JS, my apologies.
Glad you got it sorted, good job.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
How do I embed javascript code within razor?
Hello:
I have a foreach loop that I have some javascript in. It does not recognize the javascript and script, but as razor code. How do I denote that the chunk is javascript?
@foreach(var contact in Model.inquiryContacts)
{
if(selectedOption == @contact.inquiry){
alert(@contact.inquiry);
}
}
Any help would be much appreciated.
Thank you.
The <text> tags doesn't seem to work in this case.
You could use @Html.Raw:
I don't see, but trust that you are in, a <script> context.
What about the if condition? Should I put that into a string too?
Yes it is all inside <script> tags
No, your condition is part of your code, which isn't the problem - leave that as is and extend it with what I provided.
aaagghhh it's not working
Got it !
@foreach(var contact in Model.inquiryContacts)
{
@Html.Raw( "if(selectedOption == '" + @contact.inquiry.InnerText + "'){emailChangeTo = '" + @contact.email.InnerText + "';} \r\n" );
}
For some reason, the String.Format wouldn't work....but yea...HTML.Raw is the way to go !!!! Thanks so much !!!!
Oh, I didn't realise the 'if' statement was supposed to be part of the JS, my apologies.
Glad you got it sorted, good job.
is working on a reply...