0

I need to pass a param to the image onclick event. I know how to do it using a script and the controller, but I would like to know if it can be done directly in the view ? I've searched the SO and tried some examples but nothing worked so far.

@{string test = "kkkk";}
<img onclick="alert('' + @test + '')" />
<img onclick="alert(@test)" />

@{string test = "alert('kkkk')";}
<img onclick="alert(@test)" />
1
  • 2
    You are missing the ' around your vars... for example <img onclick="alert('@test')" />, this treats it as a string, otherwise its looking for a javascript object with that name... Commented Jul 9, 2019 at 15:41

2 Answers 2

2

This will give you an alert with kkkk

  </span>@{string test = "kkkk";}
    <img onclick="alert('@test')" />

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Could you please add details that support your answer? Its not clear what is wrong with the OP code none the less why your solution would work...
2

this works perfect

@{string test = "kkkk";}

<img src="" onclick="alert('@test')" />

as @Çöđěxěŕ mentioned, here is the update: The reason why this solution works, op has not used single quotation marks around the var @test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.