简体   繁体   English

如何在C#MVC中将参数传递给image onclick事件?

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

I need to pass a param to the image onclick event. 我需要将参数传递给图像onclick事件。 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. 我已经搜索过SO,并尝试了一些示例,但到目前为止没有任何效果。

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

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

This will give you an alert with kkkk 这会提醒您kkkk

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

在此处输入图片说明

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. 正如@Çöđěxěŕ所提到的,这里是更新:此解决方案起作用的原因,op并未在var @test周围使用单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM