在 MVC 5 Razor 视图中调用 JavaScript 函数 [英] Calling JavaScript function in MVC 5 Razor view
问题描述
我在另一篇文章中看到,您可以像这样在 razor 代码中调用 JavaScript 函数:
@:FunctionName()
对我来说,虽然这只会输出实际的单词 FunctionName()
这是我的观点:
@model PriceCompare.Models.QuoteModel@{ViewBag.Title = "报价";}<h2>引用</h2>@if (@Model.clarify == true){//做下拉 loic@:ShowClarify();}别的{//填写引用@:ShowQuote();}<div class="澄清">你可以看到澄清的div
<div class="quote">你可以看到报价div
@节头{<script type="text/javascript">$(文件).准备好(函数 ShowQuote() {$(".quote").show();},函数 ShowClarify() {$(".clarify").show();});}
是不是因为我把它嵌套在了‘@if’中?无论如何?
你需要把你的 javascript 放在一个 标签中,并且你需要调用它们作用域内的函数:
I have seen in another post that you can call a JavaScript function in your razor code like so:
@:FunctionName()
For me though this only outputs the actual words FunctionName()
Here is my view:
@model PriceCompare.Models.QuoteModel
@{
ViewBag.Title = "Quote";
}
<h2>Quote</h2>
@if (@Model.clarify == true)
{
// do drop down loic
@:ShowClarify();
}
else
{
// fill quote
@:ShowQuote();
}
<div class="clarify">
You can see the clarify div
</div>
<div class="quote">
You can see the quote div
</div>
@section head {
<script type="text/javascript">
$(document).ready(
function ShowQuote() {
$(".quote").show();
},
function ShowClarify() {
$(".clarify").show();
}
);
</script>
}
Is it because I have nested it in an `@if'? Anyway around this?
You need to put your javascript in a <script>
tag, and you need to call the functions within their scope:
<script type="text/javascript">
$(document).ready(
function ShowQuote() {
$(".quote").show();
},
function ShowClarify() {
$(".clarify").show();
}
@if (@Model.clarify == true)
{
// do drop down loic
ShowClarify();
}
else
{
// fill quote
ShowQuote();
}
);
</script>
这篇关于在 MVC 5 Razor 视图中调用 JavaScript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!