admin管理员组文章数量:1354748
I have a Model
with LevelInfo
property:
public IEnumerable<Tuple<string, string, string>> LevelInfo { get; set; }
In the view I have a JS function:
function fillPath(level, color) {
$('[level=' + level).attr({ 'fill': color });
}
Now I want to iterate through LevelInfo
and call fillPath
function:
$(document).ready(function() {
@foreach (var info in Model.LevelInfo)
{
// How can I call JS function from here?
// i.e, fillPath(info.Item1, info.Item2)
}
});
Thanks.
I have a Model
with LevelInfo
property:
public IEnumerable<Tuple<string, string, string>> LevelInfo { get; set; }
In the view I have a JS function:
function fillPath(level, color) {
$('[level=' + level).attr({ 'fill': color });
}
Now I want to iterate through LevelInfo
and call fillPath
function:
$(document).ready(function() {
@foreach (var info in Model.LevelInfo)
{
// How can I call JS function from here?
// i.e, fillPath(info.Item1, info.Item2)
}
});
Thanks.
Share Improve this question asked Jun 7, 2012 at 6:35 user1260827user1260827 1,5206 gold badges31 silver badges53 bronze badges 1-
You want to call
fillPath
on the server or the client? – leppie Commented Jun 7, 2012 at 6:36
1 Answer
Reset to default 8Remember, the @foreach
is executed server-side and emits HTML for things between {
and }
.
Simply write JavaScript right between the brackets.
$(document).ready(function() {
@foreach (var info in Model.LevelInfo)
{
fillPath(info.Item1, info.Item2) // Assumes this is a function defined in JavaScript elsewhere
}
});
Razor is sometimes a bit picky about recognizing a transition from server-side to client-side code. You may need to wrap the JavaScript code in <text>
to help Razor along.
本文标签: javascriptcall JS function inside razor foreach statementStack Overflow
版权声明:本文标题:javascript - call JS function inside razor foreach statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743949158a2566964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论