Ben Junior
Ben Junior

Reputation: 2589

How to pass a parameter to image onclick event in c# MVC?

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)" />

Upvotes: 0

Views: 988

Answers (2)

MattOpen
MattOpen

Reputation: 824

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.

Upvotes: 2

Amine
Amine

Reputation: 142

This will give you an alert with kkkk

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

enter image description here

Upvotes: 2

Related Questions