admin管理员组文章数量:1026989
I have a view that is generated by a model as a list of tasks. When clicking on a certain task I want my javascript function to receive a variable with a different value to execute.
@model IEnumerable<CreateAndApproveStockCode.Models.Task>
<h3>Tasks</h3>
@if (Model != null)
{
int num = 0;
foreach (var task in Model)
{
num = task.num;
<div class="tasksclick" onclick="callDetail("+num+")">
<div class="taskname">
@Html.DisplayFor(taskitem => task.name)
</div>
<div class="taskdes">
@Html.DisplayFor(taskitem => task.description)
</div>
</div>
}
}
<script type="text/javascript">
function callDetail(d)
{
var serviceUrl = "/Detail/Populate?d="+d;
var request = $.post(serviceUrl);
request.done(
function (data)
{
$("#detail").html(data);
}
);
}
</script>
The num variable receives from the task model its value and this value needs to be passed to the javascript variable d so that the detail view can be refreshed according to this value, however nothing happens.
I have a view that is generated by a model as a list of tasks. When clicking on a certain task I want my javascript function to receive a variable with a different value to execute.
@model IEnumerable<CreateAndApproveStockCode.Models.Task>
<h3>Tasks</h3>
@if (Model != null)
{
int num = 0;
foreach (var task in Model)
{
num = task.num;
<div class="tasksclick" onclick="callDetail("+num+")">
<div class="taskname">
@Html.DisplayFor(taskitem => task.name)
</div>
<div class="taskdes">
@Html.DisplayFor(taskitem => task.description)
</div>
</div>
}
}
<script type="text/javascript">
function callDetail(d)
{
var serviceUrl = "/Detail/Populate?d="+d;
var request = $.post(serviceUrl);
request.done(
function (data)
{
$("#detail").html(data);
}
);
}
</script>
The num variable receives from the task model its value and this value needs to be passed to the javascript variable d so that the detail view can be refreshed according to this value, however nothing happens.
Share Improve this question asked Jan 24, 2014 at 8:49 tvmannetjietvmannetjie 1501 gold badge2 silver badges13 bronze badges2 Answers
Reset to default 3Since num
is a C# variable and you're mixing HTML and "regular" code, you need to tell the piler that you're referencing a variable that it manages. Don't concatenate strings, just add the @
:
<div class="tasksclick" onclick="callDetail(@num)">
or, if you want to pass a string parameter (which is probably what you really want):
<div class="tasksclick" onclick="callDetail('@num')">
Just keep in mind that whenever we have to use c# code in a cshtml file, we always use @ before writing our code. Just like you have used @ before if(). Now because num is a C# variable, so use @ with it like this: @num
Secondly, since you want to pass to string value, so use '' around it. So, resultant code is:
<div class="tasksclick" onclick="callDetail('@num')">
I have a view that is generated by a model as a list of tasks. When clicking on a certain task I want my javascript function to receive a variable with a different value to execute.
@model IEnumerable<CreateAndApproveStockCode.Models.Task>
<h3>Tasks</h3>
@if (Model != null)
{
int num = 0;
foreach (var task in Model)
{
num = task.num;
<div class="tasksclick" onclick="callDetail("+num+")">
<div class="taskname">
@Html.DisplayFor(taskitem => task.name)
</div>
<div class="taskdes">
@Html.DisplayFor(taskitem => task.description)
</div>
</div>
}
}
<script type="text/javascript">
function callDetail(d)
{
var serviceUrl = "/Detail/Populate?d="+d;
var request = $.post(serviceUrl);
request.done(
function (data)
{
$("#detail").html(data);
}
);
}
</script>
The num variable receives from the task model its value and this value needs to be passed to the javascript variable d so that the detail view can be refreshed according to this value, however nothing happens.
I have a view that is generated by a model as a list of tasks. When clicking on a certain task I want my javascript function to receive a variable with a different value to execute.
@model IEnumerable<CreateAndApproveStockCode.Models.Task>
<h3>Tasks</h3>
@if (Model != null)
{
int num = 0;
foreach (var task in Model)
{
num = task.num;
<div class="tasksclick" onclick="callDetail("+num+")">
<div class="taskname">
@Html.DisplayFor(taskitem => task.name)
</div>
<div class="taskdes">
@Html.DisplayFor(taskitem => task.description)
</div>
</div>
}
}
<script type="text/javascript">
function callDetail(d)
{
var serviceUrl = "/Detail/Populate?d="+d;
var request = $.post(serviceUrl);
request.done(
function (data)
{
$("#detail").html(data);
}
);
}
</script>
The num variable receives from the task model its value and this value needs to be passed to the javascript variable d so that the detail view can be refreshed according to this value, however nothing happens.
Share Improve this question asked Jan 24, 2014 at 8:49 tvmannetjietvmannetjie 1501 gold badge2 silver badges13 bronze badges2 Answers
Reset to default 3Since num
is a C# variable and you're mixing HTML and "regular" code, you need to tell the piler that you're referencing a variable that it manages. Don't concatenate strings, just add the @
:
<div class="tasksclick" onclick="callDetail(@num)">
or, if you want to pass a string parameter (which is probably what you really want):
<div class="tasksclick" onclick="callDetail('@num')">
Just keep in mind that whenever we have to use c# code in a cshtml file, we always use @ before writing our code. Just like you have used @ before if(). Now because num is a C# variable, so use @ with it like this: @num
Secondly, since you want to pass to string value, so use '' around it. So, resultant code is:
<div class="tasksclick" onclick="callDetail('@num')">
本文标签: javascriptonclick with variable in it mvc 4Stack Overflow
版权声明:本文标题:javascript - onclick with variable in it mvc 4 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745636896a2160505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论