admin管理员组文章数量:1289363
I´m working with MVC razor and I have a small issue in the view part...
To resume, I have a loop with different elements, with each element I have two buttons, one download and another request. My problem is with the "request" one... I need that when I click on it, I post the ID of the element...
this is part of my code:
enter code here <div class="contentsection" style="border-bottom:1px solid orange;">
<table style="width:100%;">
<thead>
...
...
</thead>
<tbody>
@foreach (DownloadItem di in group.Items)
{
<tr>
<td class="bold">@di.DisplayName</td>
<td>
@using (Html.BeginForm("Download", "MyController", new { id = di.ID }, FormMethod.Post, new { style = "display:inline;" }))
{
<input type="submit" id="download" value="Download" />
}
<input type="button" id="request" value="Request" onclick="somejavascriptmethod(@(di.ID))" />
<span id="requestDate">@(di.requestDate)</span>
</td>
</tr>
}
</tbody>
</table>
My problem is that when the User click the Button "request", my function "somejavascriptmethod(id)" is called and I need the id as a parameter... ALthough I get a syntac error, I though it could work... but it does not... I dont "post" the id and I get the "500 Internal Server Error"...
thanks in advance for the Help!
I´m working with MVC razor and I have a small issue in the view part...
To resume, I have a loop with different elements, with each element I have two buttons, one download and another request. My problem is with the "request" one... I need that when I click on it, I post the ID of the element...
this is part of my code:
enter code here <div class="contentsection" style="border-bottom:1px solid orange;">
<table style="width:100%;">
<thead>
...
...
</thead>
<tbody>
@foreach (DownloadItem di in group.Items)
{
<tr>
<td class="bold">@di.DisplayName</td>
<td>
@using (Html.BeginForm("Download", "MyController", new { id = di.ID }, FormMethod.Post, new { style = "display:inline;" }))
{
<input type="submit" id="download" value="Download" />
}
<input type="button" id="request" value="Request" onclick="somejavascriptmethod(@(di.ID))" />
<span id="requestDate">@(di.requestDate)</span>
</td>
</tr>
}
</tbody>
</table>
My problem is that when the User click the Button "request", my function "somejavascriptmethod(id)" is called and I need the id as a parameter... ALthough I get a syntac error, I though it could work... but it does not... I dont "post" the id and I get the "500 Internal Server Error"...
thanks in advance for the Help!
Share Improve this question edited Aug 30, 2013 at 12:05 Mansfield 15.2k20 gold badges78 silver badges113 bronze badges asked Aug 30, 2013 at 12:03 minoyominoyo 951 gold badge3 silver badges15 bronze badges 7- Please post the output of this view (the html source) and your javascript code. – Mansfield Commented Aug 30, 2013 at 12:05
- My problem is here: onclick="somejavascriptmethod(@(di.ID))" I dont know how to "post" the ID.... – minoyo Commented Aug 30, 2013 at 12:06
- I mean add the output source to your question, so we can see what's happening. – Mansfield Commented Aug 30, 2013 at 12:11
- @minoyo, Where do you want to post? – Satpal Commented Aug 30, 2013 at 12:16
-
@minoyo what do you mean by post? You want to pass
ID
to controller? – Jaimin Commented Aug 30, 2013 at 12:27
4 Answers
Reset to default 3You are creating multiple input's with the same ID (request
). HTML ID's should be unique.
As an alternative approach to what you require, you could use a data-attribute
<input type="button" data-id="@(di.ID)" value="Request" />
And use jQuery to handle which ID you need:
$("input[type=button]").click(function() {
var id = $(this).data("id");
yourJSFunction(id);
});
http://jsfiddle/XAffc/
If your value is not an integer then you have to use it like following
<input type="button" id="request" value="Request" onclick="somejavascriptmethod('@(di.ID)')" />
Try this,
<input type="button" value="Assign" onclick="myfunction(@di.ID);" />
function myfunction(obj) {
var id = obj;
alert(id);//this way you can get your id.
}
try this
<input type="button" id="request" value="Request" onclick="somejavascriptmethod('@di.ID')" />
本文标签: javascriptPass c variable parameter in a onClick functionStack Overflow
版权声明:本文标题:javascript - Pass c# variable parameter in a onClick function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741416331a2377535.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论